-
Notifications
You must be signed in to change notification settings - Fork 0
/
svgline.go
61 lines (54 loc) · 1.3 KB
/
svgline.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
package samplewav
import (
"fmt"
"image/color"
"io"
)
type svgLine struct {
x1, y1, x2, y2 int64
rgba *color.RGBA
width uint
}
type svgLinePrinter struct {
buff string
minx, maxx, miny, maxy int64
lastwidth int64
}
func (s *svgLinePrinter) add(line *svgLine) {
s.buff += fmt.Sprintf(`<line x1="%d" y1="%d" x2="%d" y2="%d" style="stroke:#%x%x%x;stroke-width:%d"/>`,
line.x1, line.y1, line.x2, line.y2, line.rgba.R, line.rgba.G, line.rgba.B, line.width)
s.buff += "\n"
s.maxx, s.minx = maxAndMin(line.x1, line.x2, s.maxx, s.minx)
s.maxy, s.miny = maxAndMin(line.y1, line.y2, s.maxy, s.miny)
s.lastwidth = int64(line.width)
}
func (s *svgLinePrinter) save(w io.Writer) (err error) {
width := s.maxx - s.minx + s.lastwidth
height := s.maxy - s.miny
doc := fmt.Sprintf(`<?xml version="1.0"?>
<svg width="%d" height="%d"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<g transform="scale(1, -1) translate(0, -%d)">
`, width, height, height)
doc += "\n"
doc += s.buff
doc += "\n"
doc += "</g>\n"
doc += "</svg>"
_, err = w.Write([]byte(doc))
return
}
func maxAndMin(n ...int64) (max, min int64) {
max = n[0]
min = n[0]
for _, i := range n {
if i > max {
max = i
}
if i < min {
min = i
}
}
return
}