]> matita.cs.unibo.it Git - helm.git/blob - matita/lablGraphviz.ml
- pipe graphviz markup to tred before generating png/map output to get rid of
[helm.git] / matita / lablGraphviz.ml
1 (* Copyright (C) 2006, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (* $Id$ *)
27
28 open Printf
29
30 type attribute = string * string  (* <key, value> pair *)
31
32 let png_flags = "-Tpng"
33 let map_flags = "-Tcmapx"
34 let tred_cmd = "tred" (* graphviz transitive reduction filter *)
35
36 let tempfile () = Filename.temp_file "matita_" ""
37
38 class type graphviz_widget =
39   object
40     method load_graph_from_file: string -> unit
41     method connect_href:
42       (GdkEvent.Button.t -> (string * string) list -> unit) -> unit
43     method center_on_href: string -> unit
44     method as_image: GMisc.image
45     method as_viewport: GBin.viewport
46   end
47
48 class graphviz_impl ?packing gviz_cmd =
49   let viewport = GBin.viewport ?packing () in
50   let mk_gviz_cmd flags src_fname dest_fname =
51     sprintf "cat %s | %s | %s %s > %s" src_fname tred_cmd gviz_cmd flags
52       dest_fname in
53   let image =
54     GMisc.image ~packing:viewport#add ~xalign:0. ~yalign:0. ~xpad:0 ~ypad:0 ()
55   in
56   let parse_coords s =
57     match List.map float_of_string (HExtlib.split ~sep:',' s) with
58     | [x1; y1; x2; y2 ] -> x1, y1, x2, y2
59     | _ -> assert false in
60   object (self)
61     val mutable href_cb = fun _ _ -> ()
62     val mutable map = []  (* list of associative list attr name -> attr value *)
63
64     initializer
65       ignore (viewport#event#connect#button_press (fun button ->
66         (*eprintf "x: %f; y: %f;\n%!" (GdkEvent.Button.x button +. viewport#hadjustment#value) (GdkEvent.Button.y button +. viewport#vadjustment#value);*)
67         (* compute coordinates relative to image origin *)
68         let x = GdkEvent.Button.x button +. viewport#hadjustment#value in
69         let y = GdkEvent.Button.y button +. viewport#vadjustment#value in
70         (try href_cb button (self#find_href x y) with Not_found -> ());
71         false))
72
73     method load_graph_from_file fname =
74       let tmp_png = tempfile () in
75       let rc = Sys.command (mk_gviz_cmd png_flags fname tmp_png) in
76       if rc <> 0 then
77         eprintf
78           ("Graphviz command failed (exit code: %d) on the following graph:\n"
79            ^^ "%s\n%!")
80           rc (HExtlib.input_file fname);
81       image#set_file tmp_png;
82       HExtlib.safe_remove tmp_png;
83       let tmp_map = tempfile () in
84       ignore (Sys.command (mk_gviz_cmd map_flags fname tmp_map));
85       self#load_map tmp_map;
86       HExtlib.safe_remove tmp_map
87
88     method private load_map fname =
89       let areas = ref [] in
90       let p =
91         XmlPushParser.create_parser
92           { XmlPushParser.default_callbacks with
93             XmlPushParser.start_element =
94               Some (fun elt attrs ->
95                 match elt with
96                 | "area" -> areas := attrs :: !areas
97                 | _ -> ()) } in
98       XmlPushParser.parse p (`File fname);
99       map <- !areas
100
101     method private find_href x y =
102       List.find
103         (fun attrs ->
104           let x1, y1, x2, y2 = parse_coords (List.assoc "coords" attrs) in
105           x1 <= x && x <= x2 && y1 <= y && y <= y2)
106         map
107
108     method connect_href
109       (cb: GdkEvent.Button.t -> (string * string) list -> unit)
110     =
111       href_cb <- cb
112
113     method center_on_href href =
114       (*eprintf "Centering viewport on uri %s\n%!" href;*)
115       let attrs = 
116         List.find
117           (fun attrs ->
118             try List.assoc "href" attrs = href with Not_found -> false)
119           map in
120       try
121         let x1, y1, x2, y2 = parse_coords (List.assoc "coords" attrs) in
122         viewport#hadjustment#clamp_page ~lower:x1 ~upper:x2;
123         viewport#vadjustment#clamp_page ~lower:y1 ~upper:y2;
124       with Not_found -> ()
125
126     method as_image = image
127     method as_viewport = viewport
128
129   end
130
131 let factory cmd ?packing () =
132   (new graphviz_impl ?packing cmd :> graphviz_widget)
133
134 let gDot = factory "dot"
135 let gNeato = factory "neato"
136 let gTwopi = factory "twopi"
137 let gCirco = factory "circo"
138 let gFdp = factory "fdp"
139