-
Notifications
You must be signed in to change notification settings - Fork 1
/
svg_test.go
364 lines (358 loc) · 12.6 KB
/
svg_test.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package svg_test
import (
"bytes"
"cmp"
"encoding/xml"
"errors"
"io"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/alecthomas/assert/v2"
"github.com/twpayne/go-svg"
"github.com/twpayne/go-svg/svgpath"
)
func TestSimple(t *testing.T) {
for _, tc := range []struct {
name string
svg *svg.SVGElement
}{
{
name: "empty",
svg: svg.New(),
},
{
name: "simple",
svg: svg.New().WidthHeight(300, 200, svg.Number).AppendChildren(
svg.Rect().WidthHeight(100, 100, svg.Percent).Fill("red"),
svg.Circle().CXCYR(150, 80, 80, svg.Number).Fill("green"),
svg.Text().XY(150, 125, svg.Number).FontSize("60").TextAnchor("middle").Fill("white").AppendChildren(
svg.CharData("SVG"),
),
),
},
{
name: "opacity", // 3.6.1
svg: svg.New().WidthHeight(600, 175, svg.Number).ViewBox(0, 0, 1200, 350).AppendChildren(
svg.Rect().XYWidthHeight(100, 100, 1000, 150, svg.Number).Fill("blue"),
svg.Circle().CXCYR(200, 100, 50, svg.Number).Fill("red").Opacity(1),
svg.Circle().CXCYR(400, 100, 50, svg.Number).Fill("red").Opacity(0.8),
svg.Circle().CXCYR(600, 100, 50, svg.Number).Fill("red").Opacity(0.6),
svg.Circle().CXCYR(800, 100, 50, svg.Number).Fill("red").Opacity(0.4),
svg.Circle().CXCYR(1000, 100, 50, svg.Number).Fill("red").Opacity(0.2),
svg.G().Opacity(1).AppendChildren(
svg.Circle().CXCYR(182.5, 250, 50, svg.Number).Fill("red").Opacity(1),
svg.Circle().CXCYR(217.5, 250, 50, svg.Number).Fill("green").Opacity(1),
),
svg.G().Opacity(0.5).AppendChildren(
svg.Circle().CXCYR(382.5, 250, 50, svg.Number).Fill("red").Opacity(1),
svg.Circle().CXCYR(417.5, 250, 50, svg.Number).Fill("green").Opacity(1),
),
svg.G().Opacity(1).AppendChildren(
svg.Circle().CXCYR(582.5, 250, 50, svg.Number).Fill("red").Opacity(0.5),
svg.Circle().CXCYR(617.5, 250, 50, svg.Number).Fill("green").Opacity(0.5),
),
svg.G().Opacity(1).AppendChildren(
svg.Circle().CXCYR(817.5, 250, 50, svg.Number).Fill("green").Opacity(0.5),
svg.Circle().CXCYR(782.5, 250, 50, svg.Number).Fill("red").Opacity(0.5),
),
svg.G().Opacity(0.5).AppendChildren(
svg.Circle().CXCYR(982.5, 250, 50, svg.Number).Fill("red").Opacity(0.5),
svg.Circle().CXCYR(1017.5, 250, 50, svg.Number).Fill("green").Opacity(0.5),
),
),
},
{
name: "slightlyMoreComplex", // 5.1.1.
svg: svg.New().WidthHeight(5, 4, svg.CM).AppendChildren(
svg.Desc(
svg.CharData("Four separate rectangles"),
),
svg.Rect().XYWidthHeight(0.5, 0.5, 2, 1, svg.CM),
svg.Rect().XYWidthHeight(0.5, 2, 1, 1.5, svg.CM),
svg.Rect().XYWidthHeight(3, 0.5, 1.5, 2, svg.CM),
svg.Rect().XYWidthHeight(3.5, 3, 1, 0.5, svg.CM),
svg.Comment(" Show outline of viewport using 'rect' element "),
svg.Rect().XYWidthHeight(0.01, 0.01, 4.98, 3.98, svg.CM).Fill("none").Stroke("blue").StrokeWidth(svg.CM(0.02)),
),
},
{
name: "triangle01", // 9.3.1
svg: svg.New().WidthHeight(4, 4, svg.CM).ViewBox(0, 0, 400, 400).AppendChildren(
svg.Title(svg.CharData("Example triangle01- simple example of a 'path'")),
svg.Desc(svg.CharData("A path that draws a triangle")),
svg.Rect().XYWidthHeight(1, 1, 398, 398, svg.Number).Fill("none").Stroke("blue"),
svg.Path().D(svgpath.New().
MoveToAbs([]float64{100, 100}).
LineToAbs([]float64{300, 100}).
LineToAbs([]float64{200, 300}).
ClosePath(),
).Fill("red").Stroke("blue").StrokeWidth(svg.Number(3)),
),
},
{
name: "cubic01", // 9.3.6
svg: svg.New().WidthHeight(5, 4, svg.CM).ViewBox(0, 0, 500, 400).AppendChildren(
svg.Title(
svg.CharData("Example cubic01- cubic Bézier commands in path data"),
),
svg.Desc(
svg.CharData(`Picture showing a simple example of path data using both a "C" and an "S" command, along with annotations showing the control points and end points`),
),
svg.Style().Type("text/css").AppendChildren(
svg.CharData(strings.Join([]string{
".Border { fill:none; stroke:blue; stroke-width:1 }",
" .Connect { fill:none; stroke:#888888; stroke-width:2 }",
" .SamplePath { fill:none; stroke:red; stroke-width:5 }",
" .EndPoint { fill:none; stroke:#888888; stroke-width:2 }",
" .CtlPoint { fill:#888888; stroke:none }",
" .AutoCtlPoint { fill:none; stroke:blue; stroke-width:4 }",
" .Label { font-size:22; font-family:Verdana }",
}, "\n")),
),
svg.Rect().Class("Border").XYWidthHeight(1, 1, 498, 398, svg.Number),
svg.Polyline().Class("Connect").Points(svg.Points{
{100, 200},
{100, 100},
}),
svg.Polyline().Class("Connect").Points(svg.Points{
{250, 100},
{250, 200},
}),
svg.Polyline().Class("Connect").Points(svg.Points{
{250, 200},
{250, 300},
}),
svg.Polyline().Class("Connect").Points(svg.Points{
{400, 300},
{400, 200},
}),
svg.Path().Class("SamplePath").D(svgpath.New().
MoveToAbs([]float64{100, 200}).
CurveToAbs([][]float64{{100, 100}, {250, 100}, {250, 200}}...).
SCurveToAbs([][]float64{{400, 300}, {400, 200}}...),
),
svg.Circle().Class("EndPoint").CXCYR(100, 200, 10, svg.Number),
svg.Circle().Class("EndPoint").CXCYR(250, 200, 10, svg.Number),
svg.Circle().Class("EndPoint").CXCYR(400, 200, 10, svg.Number),
svg.Circle().Class("CtlPoint").CXCYR(100, 100, 10, svg.Number),
svg.Circle().Class("CtlPoint").CXCYR(250, 100, 10, svg.Number),
svg.Circle().Class("CtlPoint").CXCYR(400, 300, 10, svg.Number),
svg.Circle().Class("AutoCtlPoint").CXCYR(250, 300, 9, svg.Number),
svg.Text().Class("Label").XY(25, 70, svg.Number).AppendChildren(
svg.CharData("M100,200 C100,100 250,100 250,200"),
),
svg.Text().Class("Label").XY(325, 350, svg.Number).Style("text-anchor:middle").AppendChildren(
svg.CharData("S400,300 400,200"),
),
),
},
{
name: "line01", // 10.5
svg: svg.New().WidthHeight(12, 4, svg.CM).ViewBox(0, 0, 1200, 400).AppendChildren(
svg.Desc(
svg.CharData("Example line01 - lines expressed in user coordinates"),
),
svg.Rect().XYWidthHeight(1, 1, 1198, 398, svg.Number).Fill("none").Stroke("blue").StrokeWidth(svg.Number(2)),
svg.G().Stroke("green").AppendChildren(
svg.Line().X1Y1X2Y2(100, 300, 300, 100).StrokeWidth(svg.Number(5)),
svg.Line().X1Y1X2Y2(300, 300, 500, 100).StrokeWidth(svg.Number(10)),
svg.Line().X1Y1X2Y2(500, 300, 700, 100).StrokeWidth(svg.Number(15)),
svg.Line().X1Y1X2Y2(700, 300, 900, 100).StrokeWidth(svg.Number(20)),
svg.Line().X1Y1X2Y2(900, 300, 1100, 100).StrokeWidth(svg.Number(25)),
),
),
},
{
name: "polyline01", // 10.6
svg: svg.New().WidthHeight(12, 4, svg.CM).ViewBox(0, 0, 1200, 400).AppendChildren(
svg.Desc(
svg.CharData("Example polyline01 - increasingly larger bars"),
),
svg.Rect().XYWidthHeight(1, 1, 1198, 398, svg.Number).Fill("none").Stroke("blue").StrokeWidth(svg.Number(10)),
svg.Polyline().Fill("none").Stroke("blue").StrokeWidth(svg.Number(10)).Points(svg.Points{
{50, 375},
{150, 375},
{150, 325},
{250, 325},
{250, 375},
{350, 375},
{350, 250},
{450, 250},
{450, 375},
{550, 375},
{550, 175},
{650, 175},
{650, 375},
{750, 375},
{750, 100},
{850, 100},
{850, 375},
{950, 375},
{950, 25},
{1050, 25},
{1050, 375},
{1150, 375},
}),
),
},
{
name: "polygon01", // 10.7
svg: svg.New().WidthHeight(12, 4, svg.CM).ViewBox(0, 0, 1200, 400).AppendChildren(
svg.Desc(
svg.CharData("Example polygon01 - star and hexagon"),
),
svg.Rect().XYWidthHeight(1, 1, 1198, 398, svg.Number).Fill("none").Stroke("blue").StrokeWidth(svg.Number(2)),
svg.Polygon().Fill("red").Stroke("blue").StrokeWidth(svg.Number(10)).Points(svg.Points{
{350, 75},
{379, 161},
{469, 161},
{397, 215},
{423, 301},
{350, 250},
{277, 301},
{303, 215},
{231, 161},
{321, 161},
}),
svg.Polygon().Fill("lime").Stroke("blue").StrokeWidth(svg.Number(10)).Points(svg.Points{
{850, 75},
{958, 137.5},
{958, 262.5},
{850, 325},
{742, 262.6},
{742, 137.5},
}),
),
},
{
name: "toap01", // 11.8.1
svg: svg.New().WidthHeight(12, 3.6, svg.CM).ViewBox(0, 0, 1000, 300).AppendChildren(
svg.Defs(
svg.Path().ID("MyPath").D(svgpath.New().
MoveToAbs([]float64{100, 200}).
CurveToAbs([][]float64{{200, 100}, {300, 0}, {400, 100}}...).
CurveToAbs([][]float64{{500, 200}, {600, 300}, {700, 200}}...).
CurveToAbs([][]float64{{800, 100}, {900, 100}, {900, 100}}...),
),
),
svg.Desc(
svg.CharData("Example toap01 - simple text on a path"),
),
svg.Use().Href("#MyPath").Fill("none").Stroke("red"),
svg.Text().FontFamily("Verdana").FontSize("42.5").Fill("blue").AppendChildren(
svg.TextPath().Href("#MyPath").AppendChildren(
svg.CharData("We go up, then we go down, then up again"),
),
),
svg.Rect().XYWidthHeight(1, 1, 998, 298, svg.Number).Fill("none").Stroke("blue").StrokeWidth(svg.Number(2)),
),
},
{
name: "foreignObject", // 12.5
svg: svg.New().WidthHeight(4, 3, svg.In).AppendChildren(
svg.Desc(
svg.CharData("This example uses the 'switch' element to provide a fallback graphical representation of an paragraph, if XMHTML is not supported."),
),
svg.Switch(
svg.ForeignObject().WidthHeight(100, 50, svg.Number).RequiredExtensions("http://example.com/SVGExtensions/EmbeddedXHTML"),
svg.Text().FontSize("10").FontFamily("Verdana").AppendChildren(
svg.TSpan().XY(10, 10).AppendChildren(
svg.CharData("Here is a paragraph that"),
),
svg.TSpan().XY(10, 20).AppendChildren(
svg.CharData("requires word wrap."),
),
),
),
),
},
{
name: "marker", // 13.7.2
svg: svg.New().ViewBox(0, 0, 100, 300).AppendChildren(
svg.Defs(
svg.Marker().ID("m1").ViewBox(0, 0, 10, 10).RefXY(5, 5).MarkerWidthHeight(8, 8).AppendChildren(
svg.Circle().CXCYR(5, 5, 5, svg.Number).Fill("green"),
),
svg.Marker().ID("m2").ViewBox(0, 0, 10, 10).RefXY(5, 5).MarkerWidthHeight(6.5, 6.5).AppendChildren(
svg.Circle().CXCYR(5, 5, 5, svg.Number).Fill("skyblue").Opacity(0.9),
),
svg.Marker().ID("m3").ViewBox(0, 0, 10, 10).RefXY(5, 5).MarkerWidthHeight(5, 5).AppendChildren(
svg.Circle().CXCYR(5, 5, 5, svg.Number).Fill("maroon").Opacity(0.85),
),
),
svg.Path().D(svgpath.New().
MoveToAbs([]float64{10, 10}).
HLineToRel(10).
VLineToRel(10).
ClosePath().
MoveToRel([]float64{20, 0}).
HLineToRel(10).
VLineToRel(10).
ClosePath().
MoveToRel([]float64{20, 0}).
HLineToRel(10).
VLineToRel(10).
ClosePath(),
).Fill("none").Stroke("black").MarkerStart("url(#m1)").MarkerMid("url(#m2)").MarkerEnd("url(#m3)"),
),
},
} {
t.Run(tc.name, func(t *testing.T) {
var buffer bytes.Buffer
n, err := tc.svg.WriteToIndent(&buffer, "", " ")
assert.NoError(t, err)
actual := buffer.Bytes()
filename := filepath.Join("testdata", tc.name+".svg")
if os.Getenv("GO_SVG_WRITE_TESTDATA") == "1" {
assert.NoError(t, os.WriteFile(filename, actual, 0o666)) //nolint:gosec
} else {
expected, err := os.ReadFile(filename)
assert.NoError(t, err)
assertEquivalentXML(t, expected, actual)
assert.Equal(t, len(expected), int(n))
}
})
}
}
// assertEquivalentXML asserts that expectedBytes and actualBytes are equivalent
// XML documents.
func assertEquivalentXML(t *testing.T, expectedBytes, actualBytes []byte) {
t.Helper()
expectedTokens, err := normalizedXMLTokens(expectedBytes)
assert.NoError(t, err)
actualTokens, err := normalizedXMLTokens(actualBytes)
assert.NoError(t, err)
assert.Equal(t, expectedTokens, actualTokens)
}
// normalizedXMLTokens returns all the XML tokens in data, with attributes
// sorted in order and [xml.CharData]s converted to strings.
func normalizedXMLTokens(data []byte) ([]any, error) {
var tokens []any
decoder := xml.NewDecoder(bytes.NewReader(data))
for {
switch token, err := decoder.Token(); {
case errors.Is(err, io.EOF):
return tokens, nil
case err != nil:
return nil, err
default:
token = xml.CopyToken(token)
if charData, ok := token.(xml.CharData); ok {
token = string(bytes.TrimSpace(charData))
} else if startElement, ok := token.(xml.StartElement); ok {
slices.SortFunc(startElement.Attr, func(a, b xml.Attr) int {
return cmp.Or(
cmp.Compare(a.Name.Space, b.Name.Space),
cmp.Compare(a.Name.Local, b.Name.Local),
cmp.Compare(a.Value, b.Value),
)
})
token = startElement
}
tokens = append(tokens, token)
}
}
}