Skip to content

Commit

Permalink
remove pango dep of layout and move it back in ocamlgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
giltho committed May 3, 2020
1 parent 0717d33 commit 4f6e145
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 80 deletions.
6 changes: 4 additions & 2 deletions dgraph/dGraphContainer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(* This file is part of OcamlGraph. *)
(* *)
(* Copyright (C) 2009-2010 *)
(* CEA (Commissariat à l'Énergie Atomique) *)
(* CEA (Commissariat l'�nergie Atomique) *)
(* *)
(* you can redistribute it and/or modify it under the terms of the GNU *)
(* Lesser General Public License as published by the Free Software *)
Expand Down Expand Up @@ -394,8 +394,10 @@ module Make(G: Graphviz.GraphWithDotAttrs) = struct
~status
~mk_global_view:(fun () -> mk_global_view (GlobalModel.from_graph g))
~mk_tree_view:(fun ~depth_backward ~depth_forward w v ->
let context = GtkBase.Widget.create_pango_context w in
let fontMeasure = PangoMeasure.withContext ~context in
let model =
FullTreeModel.from_graph ~depth_forward ~depth_backward w g v
FullTreeModel.from_graph ~depth_forward ~depth_backward ~fontMeasure g v
in
mk_tree_view model)
root
Expand Down
6 changes: 4 additions & 2 deletions dgraph/dGraphViewItem.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(* This file is part of OcamlGraph. *)
(* *)
(* Copyright (C) 2009-2010 *)
(* CEA (Commissariat à l'Énergie Atomique) *)
(* CEA (Commissariat l'�nergie Atomique) *)
(* *)
(* you can redistribute it and/or modify it under the terms of the GNU *)
(* Lesser General Public License as published by the Free Software *)
Expand Down Expand Up @@ -289,7 +289,9 @@ let text draw_st group (x,y) _align anchor label =
let anchor =
if anchor = -. 1. then `WEST else if anchor = 1.0 then `EAST else `CENTER
in
let size_points,height = XDotDraw.string_scale_size font size_points label
let size_points,height = XDotDraw.string_scale_size
~fontMeasure:(PangoMeasure.withContext ~context:(Gdk.Screen.get_pango_context ()))
font size_points label
in
(* y-height/4 because the base line of the text is 1/4th from the bottom *)
graph_text
Expand Down
9 changes: 9 additions & 0 deletions dgraph/pangoMeasure.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let withContext ~context ~fontName ~fontSize text =
let font_description = Pango.Font.from_string fontName in
Pango.Font.modify font_description
~size:(fontSize * Pango.scale)
();
Pango.Context.set_font_description context font_description;
let layout = Pango.Layout.create context in
Pango.Layout.set_text layout text;
Pango.Layout.get_pixel_size layout
1 change: 1 addition & 0 deletions dgraph/pangoMeasure.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val withContext : context:Pango.context -> fontName:string -> fontSize:int -> string -> int * int
1 change: 0 additions & 1 deletion dgraph/dGraphModel.ml → src/dGraphModel.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

(* This graph model is for now immutable, no adding or removing nodes. *)

open Graph
open XDot

exception DotError of string
Expand Down
5 changes: 2 additions & 3 deletions dgraph/dGraphModel.mli → src/dGraphModel.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(* This file is part of OcamlGraph. *)
(* *)
(* Copyright (C) 2009-2010 *)
(* CEA (Commissariat à l'Énergie Atomique) *)
(* CEA (Commissariat l'�nergie Atomique) *)
(* *)
(* you can redistribute it and/or modify it under the terms of the GNU *)
(* Lesser General Public License as published by the Free Software *)
Expand All @@ -26,7 +26,6 @@
(** Abstract graph model *)

open XDot
open Graph

exception DotError of string

Expand Down Expand Up @@ -63,7 +62,7 @@ class type ['vertex, 'edge, 'cluster] abstract_model = object
end

(** This functor creates a model from a graph *)
module Make(G : Graph.Graphviz.GraphWithDotAttrs) : sig
module Make(G : Graphviz.GraphWithDotAttrs) : sig

type cluster = string
exception Multiple_layouts of (G.E.t * edge_layout) list
Expand Down
2 changes: 0 additions & 2 deletions dgraph/dGraphRandModel.ml → src/dGraphRandModel.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
(* *)
(**************************************************************************)

open Graph

let element = function
| [] -> invalid_arg "empty list in element"
| l ->
Expand Down
6 changes: 3 additions & 3 deletions dgraph/dGraphRandModel.mli → src/dGraphRandModel.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(* This file is part of OcamlGraph. *)
(* *)
(* Copyright (C) 2009-2010 *)
(* CEA (Commissariat à l'Énergie Atomique) *)
(* CEA (Commissariat l'�nergie Atomique) *)
(* *)
(* you can redistribute it and/or modify it under the terms of the GNU *)
(* Lesser General Public License as published by the Free Software *)
Expand All @@ -23,10 +23,10 @@
(* *)
(**************************************************************************)

module G: Graph.Sig.G
module G: Sig.G

module GraphAttrs:
Graph.Graphviz.GraphWithDotAttrs with type t = G.t
Graphviz.GraphWithDotAttrs with type t = G.t
and type V.t = G.V.t
and type E.t = G.E.t

Expand Down
5 changes: 2 additions & 3 deletions dgraph/dGraphSubTree.ml → src/dGraphSubTree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(* This file is part of OcamlGraph. *)
(* *)
(* Copyright (C) 2009-2010 *)
(* CEA (Commissariat à l'Énergie Atomique) *)
(* CEA (Commissariat l'�nergie Atomique) *)
(* *)
(* you can redistribute it and/or modify it under the terms of the GNU *)
(* Lesser General Public License as published by the Free Software *)
Expand All @@ -23,8 +23,6 @@
(* *)
(**************************************************************************)

open Graph

module type G = sig
type t
module V : sig
Expand Down Expand Up @@ -253,6 +251,7 @@ struct
Queue.add (root, 0) q;
loop ()
*)

(** Build a tree graph centered on a vertex and containing its
predecessors and successors *)
let make src_graph src_vertex depth_forward depth_backward =
Expand Down
4 changes: 1 addition & 3 deletions dgraph/dGraphSubTree.mli → src/dGraphSubTree.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(* This file is part of OcamlGraph. *)
(* *)
(* Copyright (C) 2009-2010 *)
(* CEA (Commissariat à l'Énergie Atomique) *)
(* CEA (Commissariat l'�nergie Atomique) *)
(* *)
(* you can redistribute it and/or modify it under the terms of the GNU *)
(* Lesser General Public License as published by the Free Software *)
Expand All @@ -23,8 +23,6 @@
(* *)
(**************************************************************************)

open Graph

module type G = sig
type t
module V : sig
Expand Down
32 changes: 9 additions & 23 deletions dgraph/dGraphTreeLayout.ml → src/dGraphTreeLayout.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(* This file is part of OcamlGraph. *)
(* *)
(* Copyright (C) 2009-2010 *)
(* CEA (Commissariat à l'Énergie Atomique) *)
(* CEA (Commissariat l'�nergie Atomique) *)
(* *)
(* you can redistribute it and/or modify it under the terms of the GNU *)
(* Lesser General Public License as published by the Free Software *)
Expand All @@ -23,8 +23,6 @@
(* *)
(**************************************************************************)

open Graph

let set_if_none field value = match field with
| None -> Some value
| Some a -> Some a
Expand Down Expand Up @@ -307,38 +305,26 @@ struct

(* Calculate dimension of a string in pixel *)
let calc_dimensions
family
~fontMeasure
font
ptsize
?(weight=`NORMAL)
?(style=`NORMAL)
s
context_obj
=
let width_margin = 20. in
let height_margin = 0. in
let font_description = Pango.Font.from_string "" in
Pango.Font.modify font_description
~family:family
~weight
~style
~size:(ptsize * Pango.scale)
();
let context = GtkBase.Widget.create_pango_context context_obj in
Pango.Context.set_font_description context font_description;
let layout = Pango.Layout.create context in
Pango.Layout.set_text layout s;
let width, height = Pango.Layout.get_pixel_size layout in
let width, height = fontMeasure ~fontName:font ~fontSize:ptsize s in
float width +. width_margin, float height +. height_margin

let fill_dimensions context tree vattributes geometry_info =
let fill_dimensions ~fontMeasure tree vattributes geometry_info =
let add_vertex_dimensions v =
let vattrs = try HV.find vattributes v with Not_found -> assert false in
let minwidth, minheight = the vattrs.width, the vattrs.height in
let truewidth, trueheight =
calc_dimensions
~fontMeasure
(the vattrs.fontname)
(the vattrs.fontsize)
(the vattrs.label) context
(the vattrs.label)
in
let width = max minwidth truewidth in
let height = max minheight trueheight in
Expand Down Expand Up @@ -600,7 +586,7 @@ struct
}

(* Graph *)
let from_tree context tree root =
let from_tree ~fontMeasure tree root =
let vattributes = HV.create 97 in
fill_vattributes tree vattributes;
let geometry_info =
Expand All @@ -609,7 +595,7 @@ struct
x_offset = 0.;
y_offset = 0 }
in
fill_dimensions context tree vattributes geometry_info;
fill_dimensions ~fontMeasure tree vattributes geometry_info;
set_offset geometry_info;
fill_position tree root geometry_info;

Expand Down
8 changes: 4 additions & 4 deletions dgraph/dGraphTreeLayout.mli → src/dGraphTreeLayout.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(* This file is part of OcamlGraph. *)
(* *)
(* Copyright (C) 2009-2010 *)
(* CEA (Commissariat à l'Énergie Atomique) *)
(* CEA (Commissariat l'�nergie Atomique) *)
(* *)
(* you can redistribute it and/or modify it under the terms of the GNU *)
(* Lesser General Public License as published by the Free Software *)
Expand All @@ -23,8 +23,6 @@
(* *)
(**************************************************************************)

open Graph

type cluster = string

module Make
Expand All @@ -33,7 +31,9 @@ module Make
sig

val from_tree:
[> `widget] Gtk.obj -> Tree.t -> Tree.V.t -> XDot.Make(Tree).graph_layout
fontMeasure:(
fontName:string -> fontSize:int -> string -> int * int
) -> Tree.t -> Tree.V.t -> XDot.Make(Tree).graph_layout

end

Expand Down
8 changes: 3 additions & 5 deletions dgraph/dGraphTreeModel.ml → src/dGraphTreeModel.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(* This file is part of OcamlGraph. *)
(* *)
(* Copyright (C) 2009-2010 *)
(* CEA (Commissariat à l'Énergie Atomique) *)
(* CEA (Commissariat l'�nergie Atomique) *)
(* *)
(* you can redistribute it and/or modify it under the terms of the GNU *)
(* Lesser General Public License as published by the Free Software *)
Expand All @@ -23,8 +23,6 @@
(* *)
(**************************************************************************)

open Graph

module type S = sig

module Tree: Graphviz.GraphWithDotAttrs
Expand Down Expand Up @@ -208,7 +206,7 @@ module SubTreeMake(G: Graphviz.GraphWithDotAttrs) = struct
let from_graph
?(depth_forward=2)
?(depth_backward=2)
context
~fontMeasure
g
v
=
Expand All @@ -217,7 +215,7 @@ module SubTreeMake(G: Graphviz.GraphWithDotAttrs) = struct
tree_ref := Some t;
graph_ref := Some g;
let layout =
TreeLayout.from_tree context (TM.get_structure t) (TM.get_root t)
TreeLayout.from_tree ~fontMeasure (TM.get_structure t) (TM.get_root t)
in
new tree_model layout t

Expand Down
10 changes: 6 additions & 4 deletions dgraph/dGraphTreeModel.mli → src/dGraphTreeModel.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(* This file is part of OcamlGraph. *)
(* *)
(* Copyright (C) 2009-2010 *)
(* CEA (Commissariat à l'Énergie Atomique) *)
(* CEA (Commissariat l'�nergie Atomique) *)
(* *)
(* you can redistribute it and/or modify it under the terms of the GNU *)
(* Lesser General Public License as published by the Free Software *)
Expand All @@ -25,7 +25,7 @@

module type S = sig

module Tree: Graph.Graphviz.GraphWithDotAttrs
module Tree: Graphviz.GraphWithDotAttrs

module TreeManipulation : sig
type t
Expand All @@ -49,13 +49,15 @@ module type S = sig
end

(** This functor creates a model centered on a vertex from a graph *)
module SubTreeMake(G : Graph.Graphviz.GraphWithDotAttrs) : sig
module SubTreeMake(G : Graphviz.GraphWithDotAttrs) : sig

include S with type Tree.V.label = G.V.t

val from_graph :
?depth_forward:int -> ?depth_backward:int ->
[> `widget] Gtk.obj -> G.t -> G.V.t -> tree_model
fontMeasure:(
fontName:string -> fontSize:int -> string -> int * int
) -> G.t -> G.V.t -> tree_model

end

Expand Down
9 changes: 8 additions & 1 deletion src/graph.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ module Merge = Merge
module Mincut = Mincut
module Clique = Clique
module WeakTopological = WeakTopological
module ChaoticIteration = ChaoticIteration
module ChaoticIteration = ChaoticIteration
module XDotDraw = XDotDraw
module XDot = XDot
module DGraphModel = DGraphModel
module DGraphTreeLayout = DGraphTreeLayout
module DGraphSubTree = DGraphSubTree
module DGraphTreeModel = DGraphTreeModel
module DGraphRandModel = DGraphRandModel
5 changes: 2 additions & 3 deletions dgraph/xDot.ml → src/xDot.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(* This file is part of OcamlGraph. *)
(* *)
(* Copyright (C) 2009-2010 *)
(* CEA (Commissariat à l'Énergie Atomique) *)
(* CEA (Commissariat l'�nergie Atomique) *)
(* *)
(* you can redistribute it and/or modify it under the terms of the GNU *)
(* Lesser General Public License as published by the Free Software *)
Expand All @@ -25,7 +25,6 @@

(** Reading XDot files *)

open Graph
open Dot_ast
open Printf

Expand Down Expand Up @@ -197,7 +196,7 @@ let read_bounding_box str =
let lower_left = (x1, -.y2) and upper_right = x2, -.y1 in
lower_left,upper_right

module Make(G : Graph.Graphviz.GraphWithDotAttrs) = struct
module Make(G : Graphviz.GraphWithDotAttrs) = struct

module HV = Hashtbl.Make(G.V)

Expand Down
Loading

0 comments on commit 4f6e145

Please sign in to comment.