-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode.go
84 lines (68 loc) · 1.74 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package layout
import (
"strings"
)
type Node struct {
ID string
Label string
Weight float64
Tooltip string
FontName string
FontSize Length
FontColor Color
LineWidth Length
LineColor Color
Shape Shape
FillColor Color
Radius Vector
// computed in layouting
Center Vector
}
func NewNode(id string) *Node {
node := &Node{}
node.ID = id
node.Weight = 1.0
node.LineWidth = Point
return node
}
func (node *Node) String() string {
if node == nil {
return "?"
}
if node.ID != "" {
return node.ID
}
return node.Label
}
func (node *Node) DefaultLabel() string {
if node.Label != "" {
return node.Label
}
return node.ID
}
func (node *Node) approxLabelRadius(lineHeight Length) Vector {
const HeightWidthRatio = 0.5
if lineHeight < node.FontSize {
lineHeight = node.FontSize
}
size := Vector{}
lines := strings.Split(node.DefaultLabel(), "\n")
for _, line := range lines {
width := Length(len(line)) * node.FontSize * HeightWidthRatio
if width > size.X {
size.X = width
}
size.Y += lineHeight
}
size.X *= 0.5
size.Y = Length(len(lines)) * lineHeight * 0.5
return size
}
func (node *Node) TopLeft() Vector { return Vector{node.Left(), node.Top()} }
func (node *Node) BottomRight() Vector { return Vector{node.Right(), node.Bottom()} }
func (node *Node) TopCenter() Vector { return Vector{node.Center.X, node.Top()} }
func (node *Node) BottomCenter() Vector { return Vector{node.Center.X, node.Bottom()} }
func (node *Node) Left() Length { return node.Center.X - node.Radius.X }
func (node *Node) Top() Length { return node.Center.Y - node.Radius.Y }
func (node *Node) Right() Length { return node.Center.X + node.Radius.X }
func (node *Node) Bottom() Length { return node.Center.Y + node.Radius.Y }