forked from goccy/go-graphviz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphviz.go
139 lines (119 loc) · 2.65 KB
/
graphviz.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package graphviz
import (
"image"
"io"
"github.com/goccy/go-graphviz/cgraph"
"github.com/goccy/go-graphviz/gvc"
"golang.org/x/image/font"
)
type Graphviz struct {
ctx *gvc.Context
name string
dir *cgraph.Desc
layout Layout
}
type Layout string
const (
CIRCO Layout = "circo"
DOT Layout = "dot"
FDP Layout = "fdp"
NEATO Layout = "neato"
OSAGE Layout = "osage"
PATCHWORK Layout = "patchwork"
SFDP Layout = "sfdp"
TWOPI Layout = "twopi"
)
type Format string
const (
XDOT Format = "dot"
SVG Format = "svg"
PNG Format = "png"
JPG Format = "jpg"
)
func ParseFile(path string) (*cgraph.Graph, error) {
graph, err := cgraph.ParseFile(path)
if err != nil {
return nil, err
}
return graph, nil
}
func ParseBytes(bytes []byte) (*cgraph.Graph, error) {
graph, err := cgraph.ParseBytes(bytes)
if err != nil {
return nil, err
}
return graph, nil
}
func New() *Graphviz {
return &Graphviz{
ctx: gvc.New(),
dir: cgraph.Directed,
layout: DOT,
}
}
func (g *Graphviz) Close() error {
return g.ctx.Close()
}
func (g *Graphviz) SetLayout(layout Layout) *Graphviz {
g.layout = layout
return g
}
func (g *Graphviz) SetFontFace(callback func(size float64) (font.Face, error)) {
gvc.SetFontFace(callback)
}
func (g *Graphviz) SetRenderer(format Format, renderer gvc.Renderer) {
gvc.RegisterRenderer(string(format), renderer)
}
func (g *Graphviz) Render(graph *cgraph.Graph, format Format, w io.Writer) (e error) {
if err := g.ctx.Layout(graph, string(g.layout)); err != nil {
return err
}
defer func() {
if err := g.ctx.FreeLayout(graph); err != nil {
e = err
}
}()
if err := g.ctx.RenderData(graph, string(format), w); err != nil {
return err
}
return nil
}
func (g *Graphviz) RenderImage(graph *cgraph.Graph) (img image.Image, e error) {
if err := g.ctx.Layout(graph, string(g.layout)); err != nil {
return nil, err
}
defer func() {
if err := g.ctx.FreeLayout(graph); err != nil {
e = err
}
}()
image, err := g.ctx.RenderImage(graph, string(PNG))
if err != nil {
return nil, err
}
return image, nil
}
func (g *Graphviz) RenderFilename(graph *cgraph.Graph, format Format, path string) (e error) {
if err := g.ctx.Layout(graph, string(g.layout)); err != nil {
return err
}
defer func() {
if err := g.ctx.FreeLayout(graph); err != nil {
e = err
}
}()
if err := g.ctx.RenderFilename(graph, string(format), path); err != nil {
return err
}
return nil
}
func (g *Graphviz) Graph(option ...GraphOption) (*cgraph.Graph, error) {
for _, opt := range option {
opt(g)
}
graph, err := cgraph.Open(g.name, g.dir, nil)
if err != nil {
return nil, err
}
return graph, nil
}