forked from mitroadmaps/gomapinfer
/
svg.go
242 lines (223 loc) · 5.59 KB
/
svg.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package common
import (
"github.com/ajstarks/svgo"
"fmt"
"math"
"math/rand"
"os"
)
type WeightedBoundable struct {
Boundable Boundable
Weight float64
}
func (wb WeightedBoundable) Bounds() Rectangle {
return wb.Boundable.Bounds()
}
type WidthBoundable struct {
Boundable Boundable
Width float64
}
func (b WidthBoundable) Bounds() Rectangle {
return b.Boundable.Bounds()
}
type ColoredBoundable struct {
Boundable Boundable
Color string
}
func (b ColoredBoundable) Bounds() Rectangle {
return b.Boundable.Bounds()
}
type SvgLabel struct {
Point Point
Text string
}
func (b SvgLabel) Bounds() Rectangle {
return b.Point.Bounds()
}
type EmbeddedImage struct {
Src Point
Dst Point
Image string
}
func (img EmbeddedImage) Bounds() Rectangle {
return EmptyRectangle.Extend(img.Src).Extend(img.Dst)
}
type WrappingBoundable interface {
Unwrap() Boundable
}
type SVGOptions struct {
Scale float64
Zoom float64
ScaleX float64
ScaleY float64
Sparse float64
Bounds Rectangle
Blur float64
StrokeWidth float64
Color string
Unflip bool
}
func CreateSVG(fname string, elements [][]Boundable, options SVGOptions) error {
file, err := os.Create(fname)
if err != nil {
return err
}
defer file.Close()
// get bounds over all elements
var r Rectangle
if options.Bounds.Max.X > options.Bounds.Min.X {
r = options.Bounds
} else {
r = EmptyRectangle
for _, l := range elements {
for _, element := range l {
r = r.ExtendRect(element.Bounds())
}
}
}
// autoscale if requested
scaleFactor := options.Scale
if scaleFactor == 0 {
l := math.Max(r.Lengths().X, r.Lengths().Y)
scaleFactor = 1000 / l
}
scale := Point{scaleFactor, scaleFactor}
if options.ScaleX != 0 && options.ScaleY != 0 {
scale.X = options.ScaleX
scale.Y = options.ScaleY
}
if options.Zoom != 0 {
scale = scale.Scale(options.Zoom)
}
strokeWidth := options.StrokeWidth
if strokeWidth == 0 {
strokeWidth = 2
}
canvas := svg.New(file)
length := r.Lengths().MulPairwise(scale)
canvas.Start(int(length.X) + 1, int(length.Y) + 1)
canvas.Rect(0, 0, int(length.X) + 1, int(length.Y) + 1, "fill:white")
if options.Blur > 0 {
canvas.Filter("blur")
canvas.FeGaussianBlur(svg.Filterspec{In: "SourceGraphic"}, options.Blur, options.Blur)
canvas.Fend()
canvas.Group("filter=\"url(#blur)\"")
canvas.Rect(0, 0, int(length.X) + 1, int(length.Y) + 1, "fill:white")
}
transform := func(point Point) (int, int) {
p := point.Sub(r.Min).MulPairwise(scale)
if options.Unflip {
return int(p.X), int(p.Y)
} else {
return int(p.X), int(length.Y - p.Y)
}
}
var drawElement func(element Boundable, color string, weight float64, width float64) error
drawElement = func(element Boundable, color string, weight float64, width float64) error {
switch element := element.(type) {
case Point:
if !r.Contains(element) {
return nil
}
x, y := transform(element)
style := fmt.Sprintf("fill:%s", color)
if weight != 1 {
style += fmt.Sprintf(";opacity:%f", weight)
}
canvas.Circle(x, y, int(width), style)
case Segment:
if !r.Intersects(element.Bounds()) {
return nil
}
srcX, srcY := transform(element.Start)
dstX, dstY := transform(element.End)
style := fmt.Sprintf("stroke:%s;stroke-width:%f", color, width)
if weight != 1 {
style += fmt.Sprintf(";opacity:%f", weight)
}
canvas.Line(srcX, srcY, dstX, dstY, style)
case Rectangle:
if !r.Intersects(element) {
return nil
}
srcX, srcY := transform(element.Min)
dstX, dstY := transform(element.Max)
var minX, minY int
var maxX, maxY int
if srcX < dstX {
minX = srcX
maxX = dstX
} else {
minX = dstX
maxX = srcX
}
if srcY < dstY {
minY = srcY
maxY = dstY
} else {
minY = dstY
maxY = srcY
}
style := fmt.Sprintf("fill:%s", color)
if weight != 1 {
style += fmt.Sprintf(";opacity:%f", weight)
}
canvas.Rect(minX, minY, maxX - minX, maxY - minY, style)
case *Graph:
for _, edge := range element.Edges {
if err := drawElement(edge.Segment(), color, weight, width); err != nil {
return err
}
}
case Traces:
for _, trace := range element {
for j := 1; j < len(trace.Observations); j++ {
if err := drawElement(Segment{trace.Observations[j - 1].Point, trace.Observations[j].Point}, color, weight, width); err != nil {
return err
}
}
}
case SvgLabel:
style := fmt.Sprintf("fill:%s;font-size:%f", color, width)
x, y := transform(element.Point)
canvas.Text(x, y, element.Text, style)
case WeightedBoundable:
return drawElement(element.Boundable, color, element.Weight, width)
case WidthBoundable:
return drawElement(element.Boundable, color, weight, element.Width)
case ColoredBoundable:
return drawElement(element.Boundable, element.Color, weight, width)
case EmbeddedImage:
srcX, srcY := transform(element.Src)
dstX, dstY := transform(element.Dst)
canvas.Image(srcX, srcY, dstX - srcX, dstY - srcY, element.Image)
case WrappingBoundable:
return drawElement(element.Unwrap(), color, weight, width)
default:
return fmt.Errorf("failed to process an element: %v", element)
}
return nil
}
var colors []string
if options.Color != "" {
colors = []string{options.Color}
} else {
colors = []string{"red", "blue", "green", "purple", "olive", "gray"}
}
for i, l := range elements {
color := colors[i % len(colors)]
for _, element := range l {
if options.Sparse > 0 && rand.Float64() >= options.Sparse {
continue
}
if err := drawElement(element, color, 1, strokeWidth); err != nil {
return err
}
}
}
if options.Blur > 0 {
canvas.Gend()
}
canvas.End()
return nil
}