This repository has been archived by the owner on Oct 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg.go
254 lines (216 loc) · 5.69 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
243
244
245
246
247
248
249
250
251
252
253
254
package jdenticon
import (
"encoding/xml"
"fmt"
"math"
"strings"
)
type SVG struct {
XMLName xml.Name `xml:"svg"`
Width int `xml:"width,attr"`
Height int `xml:"height,attr"`
PreserveAspectRatio string `xml:"preserveAspectRatio,attr"`
ViewBox string `xml:"viewBox,attr"`
Namespace string `xml:"xmlns,attr"`
Paths `xml:"path"`
}
// -----------------------------------------------------------------------------
type Paths []Path
// -----------------------------------------------------------------------------
type Path struct {
XMLName xml.Name `xml:"path"`
Shapes Shapes `xml:"d,attr"`
Fill string `xml:"fill,attr,omitempty"`
Opacity float64 `xml:"opacity,attr,omitempty"`
Stroke string `xml:"stroke,attr,omitempty"`
UseOpacity bool `xml:"-"`
}
func (p Path) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if p.UseOpacity && p.Opacity == 0 {
return nil
}
if len(p.Fill) > 0 {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "fill"}, Value: p.Fill})
}
if len(p.Stroke) > 0 {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "stroke"}, Value: p.Stroke})
}
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "d"}, Value: p.Shapes.String()})
if p.UseOpacity {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "opacity"}, Value: fmt.Sprintf("%f", p.Opacity)})
}
start.End()
if err := e.EncodeToken(start); err != nil {
return err
}
if err := e.EncodeToken(start.End()); err != nil {
return err
}
// flush to ensure tokens are written
return e.Flush()
}
// -----------------------------------------------------------------------------
type Shapes []Shape
func (shapes Shapes) String() string {
paths := []string{}
for _, shape := range shapes {
paths = append(paths, shape.Path())
}
return strings.Join(paths, "")
}
// -----------------------------------------------------------------------------
type Shape interface {
Path() string
Rotate(deg float64, center *Point)
Translate(dx, dy float64)
Copy() Shape
}
// -----------------------------------------------------------------------------
type Point struct {
X float64
Y float64
}
func (p *Point) Path() string {
return fmt.Sprintf("%.f,%.f", p.X, p.Y)
}
func (p *Point) Translate(dx, dy float64) {
p.X += dx
p.Y += dy
}
// -----------------------------------------------------------------------------
type Circle struct {
Center Point
Radius float64
Clockwise bool
}
func (c *Circle) Path() string {
var path string
arc1 := fmt.Sprintf("a%.1f,%.1f 0 1,1 %.1f,0", c.Radius, c.Radius, c.Radius*2)
arc2 := fmt.Sprintf("a%.1f,%.1f 0 1,1 -%.1f,0", c.Radius, c.Radius, c.Radius*2)
if !c.Clockwise {
position := fmt.Sprintf("M%.f,%.f", c.Center.X-c.Radius, c.Center.Y)
path = position + arc1 + arc2
} else {
position := fmt.Sprintf("M%.f,%.f", c.Center.X+c.Radius, c.Center.Y)
path = position + arc2 + arc1
}
return path
}
func (c *Circle) Translate(dx, dy float64) {
c.Center.Translate(dx, dy)
}
func (c *Circle) Rotate(deg float64, center *Point) {
x0 := 0.0
y0 := 0.0
if center != nil {
x0 = center.X
y0 = center.Y
}
c.Center.X, c.Center.Y = rotate(deg, c.Center.X, c.Center.Y, x0, y0)
}
func (c *Circle) Copy() Shape {
result := &Circle{}
*result = *c
return result
}
func newCircle(x, y, size float64, cw bool) *Circle {
return &Circle{
Center: Point{
X: x + size/2,
Y: y + size/2,
},
Radius: size / 2,
Clockwise: cw,
}
}
// -----------------------------------------------------------------------------
type Polygon struct {
Points []Point
Clockwise bool
}
func (s *Polygon) Path() string {
if len(s.Points) == 0 {
return ""
}
points := []string{}
if s.Clockwise {
for idx := 0; idx < len(s.Points); idx++ {
points = append(points, s.Points[idx].Path())
}
} else {
for idx := len(s.Points) - 1; idx >= 0; idx-- {
points = append(points, s.Points[idx].Path())
}
}
return "M" + strings.Join(points, "L") + "Z"
}
func (s *Polygon) Translate(dx, dy float64) {
for idx := range s.Points {
s.Points[idx].Translate(dx, dy)
}
}
func (s *Polygon) Rotate(deg float64, center *Point) {
x0 := 0.0
y0 := 0.0
if center != nil {
x0 = center.X
y0 = center.Y
}
for idx := range s.Points {
p := &(s.Points[idx])
p.X, p.Y = rotate(deg, p.X, p.Y, x0, y0)
}
}
func (s *Polygon) Copy() Shape {
result := &Polygon{}
*result = *s
return result
}
func newPolygon(points []Point, cw bool) *Polygon {
return &Polygon{
points,
cw,
}
}
// -----------------------------------------------------------------------------
// nolint:unparam
func newTriangle(x, y, w, h float64, r int, cw bool) Shape {
points := []Point{
{x + w, y},
{x + w, y + h},
{x, y + h},
{x, y},
}
idx := r % 4
if len(points) == idx {
points = points[:idx]
} else {
points = append(points[:idx], points[idx+1:]...)
}
return newPolygon(points, cw)
}
// -----------------------------------------------------------------------------
func newRectangle(x, y, w, h float64, cw bool) Shape {
return newPolygon([]Point{
{x, y},
{x + w, y},
{x + w, y + h},
{x, y + h},
}, cw)
}
// -----------------------------------------------------------------------------
func newRhombus(x, y, w, h float64, cw bool) Shape {
return newPolygon([]Point{
{x + w/2, y},
{x + w, y + h/2},
{x + w/2, y + h},
{x, y + h/2},
}, cw)
}
// -----------------------------------------------------------------------------
func rotate(deg float64, x, y, x0, y0 float64) (float64, float64) {
rad := deg * math.Pi / 180
xn := x0 + (x-x0)*math.Cos(rad) - (y-y0)*math.Sin(rad)
yn := y0 + (y-y0)*math.Cos(rad) + (x-x0)*math.Sin(rad)
return xn, yn
}