-
Notifications
You must be signed in to change notification settings - Fork 1
/
svg.go
66 lines (53 loc) · 1.32 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
// Package draw provides SVG writing features.
package draw
import (
"io"
"github.com/gregoryv/nexus"
)
// NewSVG returns an empty SVG of size 100x100
func NewSVG() *SVG {
return &SVG{
width: 100,
height: 100,
Content: make([]SVGWriter, 0),
}
}
type SVG struct {
width, height int
Content []SVGWriter
}
// WriteSVG writes <svg> </svg> tags and it's content to the given
// writer.
func (s *SVG) WriteSVG(w io.Writer) error {
p, err := nexus.NewPrinter(w)
p.Printf(`<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
class="root" width="%v" height="%v">`, s.width, s.height)
for _, c := range s.Content {
p.Print("\n")
c.WriteSVG(p)
}
p.Print("</svg>")
return *err
}
func (s *SVG) Append(w ...SVGWriter) {
s.Content = append(s.Content, w...)
}
func (s *SVG) Prepend(w ...SVGWriter) {
s.Content = append(w, s.Content...)
}
func (s *SVG) Width() int { return s.width }
func (s *SVG) Height() int { return s.height }
// SetWidth sets the SVG width in pixels.
func (s *SVG) SetWidth(w int) { s.width = w }
// SetHeight sets the SVG height in pixels.
func (s *SVG) SetHeight(h int) { s.height = h }
// SetSize sets the SVG size in pixels
func (s *SVG) SetSize(width, height int) {
s.width = width
s.height = height
}
type SVGWriter interface {
WriteSVG(io.Writer) error
}