Debian Codegraph @

shiny/iconvg/encode_test.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package iconvg
6
7 import (
8 "bytes"
9 "flag"
10 "image/color"
11 "math"
12 "os"
13 "path/filepath"
14 "runtime"
15 "strconv"
16 "testing"
17
18 "golang.org/x/image/math/f32"
19 )
20
21 // updateFlag controls whether to overwrite testdata files during tests.
22 // This can be useful when adding new testdataTestCases.
23 var updateFlag = flag.Bool("update", false, "Overwrite testdata files.")
24
25 func testEncode(t *testing.T, e *Encoder, wantFilename string) {
26 got, err := e.Bytes()
27 if err != nil {
28 t.Fatalf("encoding: %v", err)
29 }
30 if *updateFlag {
31 if err := os.WriteFile(filepath.FromSlash(wantFilename), got, 0666); err != nil {
32 t.Fatalf("WriteFile: %v", err)
33 }
34 return
35 }
36 want, err := os.ReadFile(filepath.FromSlash(wantFilename))
37 if err != nil {
38 t.Fatalf("ReadFile: %v", err)
39 }
40 if !bytes.Equal(got, want) {
41 // The IconVG encoder is expected to be completely deterministic across all
42 // platforms and Go compilers, so check that we get exactly the right bytes.
43 //
44 // If we get slightly different bytes on some supported platform (for example,
45 // a new GOOS/GOARCH port, or a different but spec-compliant Go compiler) due
46 // to non-determinism in floating-point math, the encoder needs to be fixed.
47 //
48 // See golang.org/issue/43219#issuecomment-748531069.
49 t.Errorf("\ngot %d bytes (on GOOS=%s GOARCH=%s, using compiler %q):\n% x\nwant %d bytes:\n% x",
50 len(got), runtime.GOOS, runtime.GOARCH, runtime.Compiler, got, len(want), want)
51 gotDisasm, err1 := disassemble(got)
52 wantDisasm, err2 := disassemble(want)
53 if err1 == nil && err2 == nil {
54 diffLines(t, string(gotDisasm), string(wantDisasm))
55 }
56 }
57 }
58
59 func TestEncodeActionInfo(t *testing.T) {
60 for _, res := range []string{"lores", "hires"} {
61 var e Encoder
62 e.Reset(Metadata{
63 ViewBox: Rectangle{
64 Min: f32.Vec2{-24, -24},
65 Max: f32.Vec2{+24, +24},
66 },
67 Palette: DefaultPalette,
68 })
69 e.HighResolutionCoordinates = res == "hires"
70
71 e.StartPath(0, 0, -20)
72 e.AbsCubeTo(-11.05, -20, -20, -11.05, -20, 0)
73 e.RelSmoothCubeTo(8.95, 20, 20, 20)
74 e.RelSmoothCubeTo(20, -8.95, 20, -20)
75 e.AbsSmoothCubeTo(11.05, -20, 0, -20)
76 e.ClosePathRelMoveTo(2, 30)
77 e.RelHLineTo(-4)
78 e.AbsVLineTo(-2)
79 e.RelHLineTo(4)
80 e.RelVLineTo(12)
81 e.ClosePathRelMoveTo(0, -16)
82 e.RelHLineTo(-4)
83 e.RelVLineTo(-4)
84 e.RelHLineTo(4)
85 e.RelVLineTo(4)
86 e.ClosePathEndPath()
87
88 testEncode(t, &e, "testdata/action-info."+res+".ivg")
89 }
90 }
91
92 func TestEncodeArcs(t *testing.T) {
93 var e Encoder
94
95 e.SetCReg(1, false, RGBAColor(color.RGBA{0xff, 0x00, 0x00, 0xff}))
96 e.SetCReg(2, false, RGBAColor(color.RGBA{0xff, 0xff, 0x00, 0xff}))
97 e.SetCReg(3, false, RGBAColor(color.RGBA{0x00, 0x00, 0x00, 0xff}))
98 e.SetCReg(4, false, RGBAColor(color.RGBA{0x00, 0x00, 0x80, 0xff}))
99
100 e.StartPath(1, -10, 0)
101 e.RelHLineTo(-15)
102 e.RelArcTo(15, 15, 0, true, false, 15, -15)
103 e.ClosePathEndPath()
104
105 e.StartPath(2, -14, -4)
106 e.RelVLineTo(-15)
107 e.RelArcTo(15, 15, 0, false, false, -15, 15)
108 e.ClosePathEndPath()
109
110 const thirtyDegrees = 30.0 / 360
111 e.StartPath(3, -15, 30)
112 e.RelLineTo(5.0, -2.5)
113 e.RelArcTo(2.5, 2.5, -thirtyDegrees, false, true, 5.0, -2.5)
114 e.RelLineTo(5.0, -2.5)
115 e.RelArcTo(2.5, 5.0, -thirtyDegrees, false, true, 5.0, -2.5)
116 e.RelLineTo(5.0, -2.5)
117 e.RelArcTo(2.5, 7.5, -thirtyDegrees, false, true, 5.0, -2.5)
118 e.RelLineTo(5.0, -2.5)
119 e.RelArcTo(2.5, 10.0, -thirtyDegrees, false, true, 5.0, -2.5)
120 e.RelLineTo(5.0, -2.5)
121 e.AbsVLineTo(30)
122 e.ClosePathEndPath()
123
124 for largeArc := 0; largeArc <= 1; largeArc++ {
125 for sweep := 0; sweep <= 1; sweep++ {
126 e.StartPath(4, 10+8*float32(sweep), -28+8*float32(largeArc))
127 e.RelArcTo(6, 3, 0, largeArc != 0, sweep != 0, 6, 3)
128 e.ClosePathEndPath()
129 }
130 }
131
132 testEncode(t, &e, "testdata/arcs.ivg")
133 }
134
135 func TestEncodeBlank(t *testing.T) {
136 var e Encoder
137 testEncode(t, &e, "testdata/blank.ivg")
138 }
139
140 var cowbellGradients = []struct {
141 radial bool
142
143 // Linear gradient coefficients.
144 x1, y1 float32
145 x2, y2 float32
146 tx, ty float32
147
148 // Radial gradient coefficients.
149 cx, cy, r float32
150 transform f32.Aff3
151
152 stops []GradientStop
153 }{{
154 // The 0th element is unused.
155 }, {
156 radial: true,
157 cx: -102.14,
158 cy: 20.272,
159 r: 18.012,
160 transform: f32.Aff3{
161 .33050, -.50775, 65.204,
162 .17296, .97021, 16.495,
163 },
164 stops: []GradientStop{
165 {Offset: 0, Color: color.RGBA{0xed, 0xd4, 0x00, 0xff}},
166 {Offset: 1, Color: color.RGBA{0xfc, 0xe9, 0x4f, 0xff}},
167 },
168 }, {
169 radial: true,
170 cx: -97.856,
171 cy: 26.719,
172 r: 18.61,
173 transform: f32.Aff3{
174 .35718, -.11527, 51.072,
175 .044280, .92977, 7.6124,
176 },
177 stops: []GradientStop{
178 {Offset: 0, Color: color.RGBA{0xed, 0xd4, 0x00, 0xff}},
179 {Offset: 1, Color: color.RGBA{0xfc, 0xe9, 0x4f, 0xff}},
180 },
181 }, {
182 x1: -16.183,
183 y1: 35.723,
184 x2: -18.75,
185 y2: 29.808,
186 tx: 48.438,
187 ty: -.22321,
188 stops: []GradientStop{
189 {Offset: 0, Color: color.RGBA{0x39, 0x21, 0x00, 0xff}},
190 {Offset: 1, Color: color.RGBA{0x0f, 0x08, 0x00, 0xff}},
191 },
192 }}
193
194 var cowbellSVGData = []struct {
195 rgba color.RGBA
196 gradient int
197 d string
198 transform *f32.Aff3
199 }{{
200 gradient: 2,
201 d: "m5.6684 17.968l.265-4.407 13.453 19.78.301 8.304-14.019-23.677z",
202 }, {
203 gradient: 1,
204 d: "m19.299 33.482l-13.619-19.688 3.8435-2.684.0922-2.1237 4.7023-2.26 2.99 1.1274 4.56-1.4252 20.719 16.272-23.288 10.782z",
205 }, {
206 rgba: color.RGBA{0xfd * 127 / 255, 0xee * 127 / 255, 0x74 * 127 / 255, 127},
207 d: "m19.285 32.845l-13.593-19.079 3.995-2.833.1689-2.0377 1.9171-.8635 18.829 18.965-11.317 5.848z",
208 }, {
209 rgba: color.RGBA{0xc4, 0xa0, 0x00, 0xff},
210 d: "m19.211 40.055c-.11-.67-.203-2.301-.205-3.624l-.003-2.406-2.492-3.769c-3.334-5.044-11.448-17.211-9.6752-14.744.3211.447 1.6961 2.119 2.1874 2.656.4914.536 1.3538 1.706 1.9158 2.6 2.276 3.615 8.232 12.056 8.402 12.056.1 0 10.4-5.325 11.294-5.678.894-.354 11.25-4.542 11.45-4.342.506.506 1.27 7.466.761 8.08-.392.473-5.06 3.672-10.256 6.121-5.195 2.45-11.984 4.269-12.594 4.269-.421 0-.639-.338-.785-1.219z",
211 }, {
212 gradient: 3,
213 d: "m19.825 33.646c.422-.68 10.105-5.353 10.991-5.753s9.881-4.123 10.468-4.009c.512.099.844 6.017.545 6.703-.23.527-8.437 4.981-9.516 5.523-1.225.616-11.642 4.705-12.145 4.369-.553-.368-.707-6.245-.343-6.833z",
214 }, {
215 rgba: color.RGBA{0x00, 0x00, 0x00, 0xff},
216 d: "m21.982 5.8789-4.865 1.457-2.553-1.1914-5.3355 2.5743l-.015625.29688-.097656 1.8672-4.1855 2.7383.36719 4.5996.054687.0957s3.2427 5.8034 6.584 11.654c1.6707 2.9255 3.3645 5.861 4.6934 8.0938.66442 1.1164 1.2366 2.0575 1.6719 2.7363.21761.33942.40065.6121.54883.81641.07409.10215.13968.18665.20312.25976.06345.07312.07886.13374.27148.22461.27031.12752.38076.06954.54102.04883.16025-.02072.34015-.05724.55078-.10938.42126-.10427.95998-.26728 1.584-.4707 1.248-.40685 2.8317-.97791 4.3926-1.5586 3.1217-1.1614 6.1504-2.3633 6.1504-2.3633l.02539-.0098.02539-.01367s2.5368-1.3591 5.1211-2.8027c1.2922-.72182 2.5947-1.4635 3.6055-2.0723.50539-.30438.93732-.57459 1.2637-.79688.16318-.11114.29954-.21136.41211-.30273.11258-.09138.19778-.13521.30273-.32617.16048-.292.13843-.48235.1543-.78906s.01387-.68208.002-1.1094c-.02384-.8546-.09113-1.9133-.17188-2.9473-.161-2.067-.373-4.04-.373-4.04l-.021-.211-20.907-16.348zm-.209 1.1055 20.163 15.766c.01984.1875.19779 1.8625.34961 3.8066.08004 1.025.14889 2.0726.17188 2.8965.01149.41192.01156.76817-.002 1.0293-.01351.26113-.09532.47241-.0332.35938.05869-.10679.01987-.0289-.05664.0332s-.19445.14831-.34375.25c-.29859.20338-.72024.46851-1.2168.76758-.99311.59813-2.291 1.3376-3.5781 2.0566-2.5646 1.4327-5.0671 2.7731-5.0859 2.7832-.03276.01301-3.0063 1.1937-6.0977 2.3438-1.5542.5782-3.1304 1.1443-4.3535 1.543-.61154.19936-1.1356.35758-1.5137.45117-.18066.04472-.32333.07255-.41992.08594-.02937-.03686-.05396-.06744-.0957-.125-.128-.176-.305-.441-.517-.771-.424-.661-.993-1.594-1.655-2.705-1.323-2.223-3.016-5.158-4.685-8.08-3.3124-5.8-6.4774-11.465-6.5276-11.555l-.3008-3.787 4.1134-2.692.109-2.0777 4.373-2.1133 2.469 1.1523 4.734-1.4179z",
217 }}
218
219 func inv(x *f32.Aff3) f32.Aff3 {
220 invDet := 1 / (x[0]*x[4] - x[1]*x[3])
221 return f32.Aff3{
222 +x[4] * invDet,
223 -x[1] * invDet,
224 (x[1]*x[5] - x[2]*x[4]) * invDet,
225 -x[3] * invDet,
226 +x[0] * invDet,
227 (x[2]*x[3] - x[0]*x[5]) * invDet,
228 }
229 }
230
231 func TestEncodeCowbell(t *testing.T) {
232 var e Encoder
233 e.Reset(Metadata{
234 ViewBox: Rectangle{
235 Min: f32.Vec2{0, 0},
236 Max: f32.Vec2{+48, +48},
237 },
238 Palette: DefaultPalette,
239 })
240
241 for _, data := range cowbellSVGData {
242 if data.rgba != (color.RGBA{}) {
243 e.SetCReg(0, false, RGBAColor(data.rgba))
244 } else if data.gradient != 0 {
245 g := cowbellGradients[data.gradient]
246 if g.radial {
247 iform := inv(&g.transform)
248 iform[2] -= g.cx
249 iform[5] -= g.cy
250 for i := range iform {
251 iform[i] /= g.r
252 }
253 e.SetGradient(10, 10, true, iform, GradientSpreadPad, g.stops)
254 } else {
255 x1 := g.x1 + g.tx
256 y1 := g.y1 + g.ty
257 x2 := g.x2 + g.tx
258 y2 := g.y2 + g.ty
259 e.SetLinearGradient(10, 10, x1, y1, x2, y2, GradientSpreadPad, g.stops)
260 }
261 }
262
263 if err := encodePathData(&e, data.d, 0, false); err != nil {
264 t.Fatal(err)
265 }
266 }
267
268 testEncode(t, &e, "testdata/cowbell.ivg")
269 }
270
271 func TestEncodeElliptical(t *testing.T) {
272 var e Encoder
273
274 const (
275 cx, cy = -20, -10
276 rx, ry = 0, 24
277 sx, sy = 30, 15
278 )
279
280 e.SetEllipticalGradient(10, 10, cx, cy, rx, ry, sx, sy, GradientSpreadReflect, []GradientStop{
281 {Offset: 0, Color: color.RGBA{0xc0, 0x00, 0x00, 0xff}},
282 {Offset: 1, Color: color.RGBA{0x00, 0x00, 0xc0, 0xff}},
283 })
284 e.StartPath(0, -32, -32)
285 e.AbsHLineTo(+32)
286 e.AbsVLineTo(+32)
287 e.AbsHLineTo(-32)
288 e.ClosePathEndPath()
289
290 e.SetCReg(0, false, RGBAColor(color.RGBA{0xff, 0xff, 0xff, 0xff}))
291 diamond := func(x, y float32) {
292 e.StartPath(0, x-1, y)
293 e.AbsLineTo(x, y-1)
294 e.AbsLineTo(x+1, y)
295 e.AbsLineTo(x, y+1)
296 e.ClosePathEndPath()
297 }
298 diamond(cx, cy)
299 diamond(cx+rx, cy+ry)
300 diamond(cx+sx, cy+sy)
301
302 testEncode(t, &e, "testdata/elliptical.ivg")
303 }
304
305 var faviconColors = []color.RGBA{
306 {0x76, 0xe1, 0xfe, 0xff},
307 {0x38, 0x4e, 0x54, 0xff},
308 {0xff, 0xff, 0xff, 0xff},
309 {0x17, 0x13, 0x11, 0xff},
310 {0x00, 0x00, 0x00, 0x54},
311 {0xff, 0xfc, 0xfb, 0xff},
312 {0xc3, 0x8c, 0x74, 0xff},
313 {0x23, 0x20, 0x1f, 0xff},
314 }
315
316 var faviconSVGData = []struct {
317 faviconColorsIndex int
318 d string
319 }{{
320 faviconColorsIndex: 1,
321 d: "m16.092 1.002c-1.1057.01-2.2107.048844-3.3164.089844-2.3441.086758-4.511.88464-6.2832 2.1758a3.8208 3.5794 29.452 0 0 -.8947 -.6856 3.8208 3.5794 29.452 0 0 -5.0879 1.2383 3.8208 3.5794 29.452 0 0 1.5664 4.9961 3.8208 3.5794 29.452 0 0 .3593 .1758c-.2784.9536-.4355 1.9598-.4355 3.0078v20h28v-20c0-1.042-.152-2.0368-.418-2.9766a3.5794 3.8208 60.548 0 0 .43359 -.20703 3.5794 3.8208 60.548 0 0 1.5684 -4.9961 3.5794 3.8208 60.548 0 0 -5.0879 -1.2383 3.5794 3.8208 60.548 0 0 -.92969 .72461c-1.727-1.257-3.843-2.0521-6.1562-2.2148-1.1058-.078-2.2126-.098844-3.3184-.089844z",
322 }, {
323 faviconColorsIndex: 0,
324 d: "m16 3c-4.835 0-7.9248 1.0791-9.7617 2.8906-.4777-.4599-1.2937-1.0166-1.6309-1.207-.9775-.5520-2.1879-.2576-2.7051.6582-.5171.9158-.1455 2.1063.8321 2.6582.2658.1501 1.2241.5845 1.7519.7441-.3281.9946-.4863 2.0829-.4863 3.2559v20h24c-.049-7.356 0-18 0-20 0-1.209-.166-2.3308-.516-3.3496.539-.2011 1.243-.5260 1.463-.6504.978-.5519 1.351-1.7424.834-2.6582s-1.729-1.2102-2.707-.6582c-.303.1711-.978.6356-1.463 1.0625-1.854-1.724-4.906-2.7461-9.611-2.7461z",
325 }, {
326 faviconColorsIndex: 1,
327 d: "m3.0918 5.9219c-.060217.00947-.10772.020635-.14648.033203-.019384.00628-.035462.013581-.052734.021484-.00864.00395-.019118.00825-.03125.015625-.00607.00369-.011621.00781-.021484.015625-.00493.00391-.017342.015389-.017578.015625-.0002366.0002356-.025256.031048-.025391.03125a.19867 .19867 0 0 0 .26367 .28320c.0005595-.0002168.00207-.00128.00391-.00195a.19867 .19867 0 0 0 .00391 -.00195c.015939-.00517.045148-.013113.085937-.019531.081581-.012836.20657-.020179.36719.00391.1020.0152.2237.0503.3535.0976-.3277.0694-.5656.1862-.7227.3145-.1143.0933-.1881.1903-.2343.2695-.023099.0396-.039499.074216-.050781.10547-.00564.015626-.00989.029721-.013672.046875-.00189.00858-.00458.017085-.00586.03125-.0006392.00708-.0005029.014724 0 .027344.0002516.00631.00192.023197.00195.023437.0000373.0002412.0097.036937.00977.037109a.19867 .19867 0 0 0 .38477 -.039063 .19867 .19867 0 0 0 0 -.00195c.00312-.00751.00865-.015947.017578-.03125.0230-.0395.0660-.0977.1425-.1601.1530-.1250.4406-.2702.9863-.2871a.19930 .19930 0 0 0 .082031 -.019531c.12649.089206.25979.19587.39844.32422a.19867 .19867 0 1 0 .2696 -.2911c-.6099-.5646-1.1566-.7793-1.5605-.8398-.2020-.0303-.3679-.0229-.4883-.0039z",
328 }, {
329 faviconColorsIndex: 1,
330 d: "m28.543 5.8203c-.12043-.018949-.28631-.026379-.48828.00391-.40394.060562-.94869.27524-1.5586.83984a.19867 .19867 0 1 0 .26953 .29102c.21354-.19768.40814-.33222.59180-.44141.51624.023399.79659.16181.94531.28320.07652.062461.11952.12063.14258.16016.0094.016037.01458.025855.01758.033203a.19867 .19867 0 0 0 .38476 .039063c.000062-.0001719.0097-.036868.0098-.037109.000037-.0002412.0017-.017125.002-.023437.000505-.012624.000639-.020258 0-.027344-.0013-.01417-.004-.022671-.0059-.03125-.0038-.017158-.008-.031248-.01367-.046875-.01128-.031254-.02768-.067825-.05078-.10742-.04624-.079195-.12003-.17424-.23437-.26758-.11891-.097066-.28260-.18832-.49609-.25781.01785-.00328.03961-.011119.05664-.013672.16062-.024082.28561-.016738.36719-.00391.03883.00611.06556.012409.08203.017578.000833.0002613.0031.0017.0039.00195a.19867 .19867 0 0 0 .271 -.2793c-.000135-.0002016-.02515-.031014-.02539-.03125-.000236-.0002356-.01265-.011717-.01758-.015625-.0099-.00782-.01737-.01194-.02344-.015625-.01213-.00737-.02066-.011673-.0293-.015625-.01727-.0079-.03336-.013247-.05273-.019531-.03877-.012568-.08822-.025682-.14844-.035156z",
331 }, {
332 faviconColorsIndex: 2,
333 d: "m15.171 9.992a4.8316 4.8316 0 0 1 -4.832 4.832 4.8316 4.8316 0 0 1 -4.8311 -4.832 4.8316 4.8316 0 0 1 4.8311 -4.8316 4.8316 4.8316 0 0 1 4.832 4.8316z",
334 }, {
335 faviconColorsIndex: 2,
336 d: "m25.829 9.992a4.6538 4.6538 0 0 1 -4.653 4.654 4.6538 4.6538 0 0 1 -4.654 -4.654 4.6538 4.6538 0 0 1 4.654 -4.6537 4.6538 4.6538 0 0 1 4.653 4.6537z",
337 }, {
338 faviconColorsIndex: 3,
339 d: "m14.377 9.992a1.9631 1.9631 0 0 1 -1.963 1.963 1.9631 1.9631 0 0 1 -1.963 -1.963 1.9631 1.9631 0 0 1 1.963 -1.963 1.9631 1.9631 0 0 1 1.963 1.963z",
340 }, {
341 faviconColorsIndex: 3,
342 d: "m25.073 9.992a1.9631 1.9631 0 0 1 -1.963 1.963 1.9631 1.9631 0 0 1 -1.963 -1.963 1.9631 1.9631 0 0 1 1.963 -1.963 1.9631 1.9631 0 0 1 1.963 1.963z",
343 }, {
344 faviconColorsIndex: 4,
345 d: "m14.842 15.555h2.2156c.40215 0 .72590.3237.72590.7259v2.6545c0 .4021-.32375.7259-.72590.7259h-2.2156c-.40215 0-.72590-.3238-.72590-.7259v-2.6545c0-.4022.32375-.7259.72590-.7259z",
346 }, {
347 faviconColorsIndex: 5,
348 d: "m14.842 14.863h2.2156c.40215 0 .72590.3238.72590.7259v2.6546c0 .4021-.32375.7259-.72590.7259h-2.2156c-.40215 0-.72590-.3238-.72590-.7259v-2.6546c0-.4021.32375-.7259.72590-.7259z",
349 }, {
350 faviconColorsIndex: 4,
351 d: "m20 16.167c0 .838-.87123 1.2682-2.1448 1.1659-.02366 0-.04795-.6004-.25415-.5832-.50367.042-1.0959-.02-1.686-.02-.61294 0-1.2063.1826-1.6855.017-.11023-.038-.17830.5838-.26153.5816-1.2437-.033-2.0788-.3383-2.0788-1.1618 0-1.2118 1.8156-2.1941 4.0554-2.1941 2.2397 0 4.0554.9823 4.0554 2.1941z",
352 }, {
353 faviconColorsIndex: 6,
354 d: "m19.977 15.338c0 .5685-.43366.8554-1.1381 1.0001-.29193.06-.63037.096-1.0037.1166-.56405.032-1.2078.031-1.8912.031-.67283 0-1.3072 0-1.8649-.029-.30627-.017-.58943-.043-.84316-.084-.81383-.1318-1.325-.417-1.325-1.0344 0-1.1601 1.8056-2.1006 4.033-2.1006s4.033.9405 4.033 2.1006z",
355 }, {
356 faviconColorsIndex: 7,
357 d: "m18.025 13.488a2.0802 1.3437 0 0 1 -2.0802 1.3437 2.0802 1.3437 0 0 1 -2.0802 -1.3437 2.0802 1.3437 0 0 1 2.0802 -1.3437 2.0802 1.3437 0 0 1 2.0802 1.3437z",
358 }}
359
360 func TestEncodeFavicon(t *testing.T) {
361 // Set up a base color for theming the favicon, gopher blue by default.
362 pal := DefaultPalette
363 pal[0] = faviconColors[0] // color.RGBA{0x76, 0xe1, 0xfe, 0xff}
364
365 var e Encoder
366 e.Reset(Metadata{
367 ViewBox: DefaultViewBox,
368 Palette: pal,
369 })
370
371 // The favicon graphic also uses a dark version of that base color. blend
372 // is 75% dark (CReg[63]) and 25% the base color (pal[0]).
373 dark := color.RGBA{0x23, 0x1d, 0x1b, 0xff}
374 blend := BlendColor(0x40, 0xff, 0x80)
375
376 // First, set CReg[63] to dark, then set CReg[63] to the blend of that dark
377 // color with pal[0].
378 e.SetCReg(1, false, RGBAColor(dark))
379 e.SetCReg(1, false, blend)
380
381 // Check that, for the suggested palette, blend resolves to the
382 // (non-themable) SVG file's faviconColors[1].
383 got := blend.Resolve(&pal, &[64]color.RGBA{
384 63: dark,
385 })
386 want := faviconColors[1]
387 if got != want {
388 t.Fatalf("Blend:\ngot %#02x\nwant %#02x", got, want)
389 }
390
391 // Set aside the remaining, non-themable colors.
392 remainingColors := faviconColors[2:]
393
394 seenFCI2 := false
395 for _, data := range faviconSVGData {
396 adj := uint8(data.faviconColorsIndex)
397 if adj >= 2 {
398 if !seenFCI2 {
399 seenFCI2 = true
400 for i, c := range remainingColors {
401 e.SetCReg(uint8(i), false, RGBAColor(c))
402 }
403 }
404 adj -= 2
405 }
406
407 if err := encodePathData(&e, data.d, adj, true); err != nil {
408 t.Fatal(err)
409 }
410 }
411
412 testEncode(t, &e, "testdata/favicon.ivg")
413 }
414
415 func encodePathData(e *Encoder, d string, adj uint8, normalizeTo64X64 bool) error {
416 var args [7]float32
417 prevN, prevVerb := 0, byte(0)
418 for first := true; d != "z"; first = false {
419 n, verb, implicit := 0, d[0], false
420 switch d[0] {
421 case 'H', 'h', 'V', 'v':
422 n = 1
423 case 'L', 'M', 'l', 'm':
424 n = 2
425 case 'S', 's':
426 n = 4
427 case 'C', 'c':
428 n = 6
429 case 'A', 'a':
430 n = 7
431 case 'z':
432 n = 0
433 default:
434 if prevVerb == '\x00' {
435 panic("unrecognized verb")
436 }
437 n, verb, implicit = prevN, prevVerb, true
438 }
439 prevN, prevVerb = n, verb
440 if prevVerb == 'M' {
441 prevVerb = 'L'
442 } else if prevVerb == 'm' {
443 prevVerb = 'l'
444 }
445 if !implicit {
446 d = d[1:]
447 }
448
449 for i := 0; i < n; i++ {
450 nDots := 0
451 if d[0] == '.' {
452 nDots = 1
453 }
454 j := 1
455 for ; ; j++ {
456 switch d[j] {
457 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
458 continue
459 case '.':
460 nDots++
461 if nDots == 1 {
462 continue
463 }
464 }
465 break
466 }
467 f, err := strconv.ParseFloat(d[:j], 64)
468 if err != nil {
469 return err
470 }
471 args[i] = float32(f)
472 for ; d[j] == ' ' || d[j] == ','; j++ {
473 }
474 d = d[j:]
475 }
476
477 if normalizeTo64X64 {
478 // The original SVG is 32x32 units, with the top left being (0, 0).
479 // Normalize to 64x64 units, with the center being (0, 0).
480 if verb == 'A' {
481 args[0] = 2 * args[0]
482 args[1] = 2 * args[1]
483 args[2] /= 360
484 args[5] = 2*args[5] - 32
485 args[6] = 2*args[6] - 32
486 } else if verb == 'a' {
487 args[0] = 2 * args[0]
488 args[1] = 2 * args[1]
489 args[2] /= 360
490 args[5] = 2 * args[5]
491 args[6] = 2 * args[6]
492 } else if first || ('A' <= verb && verb <= 'Z') {
493 for i := range args {
494 args[i] = 2*args[i] - 32
495 }
496 } else {
497 for i := range args {
498 args[i] = 2 * args[i]
499 }
500 }
501 } else if verb == 'A' || verb == 'a' {
502 args[2] /= 360
503 }
504
505 if first {
506 first = false
507 e.StartPath(adj, args[0], args[1])
508 continue
509 }
510 switch verb {
511 case 'H':
512 e.AbsHLineTo(args[0])
513 case 'h':
514 e.RelHLineTo(args[0])
515 case 'V':
516 e.AbsVLineTo(args[0])
517 case 'v':
518 e.RelVLineTo(args[0])
519 case 'L':
520 e.AbsLineTo(args[0], args[1])
521 case 'l':
522 e.RelLineTo(args[0], args[1])
523 case 'm':
524 e.ClosePathRelMoveTo(args[0], args[1])
525 case 'S':
526 e.AbsSmoothCubeTo(args[0], args[1], args[2], args[3])
527 case 's':
528 e.RelSmoothCubeTo(args[0], args[1], args[2], args[3])
529 case 'C':
530 e.AbsCubeTo(args[0], args[1], args[2], args[3], args[4], args[5])
531 case 'c':
532 e.RelCubeTo(args[0], args[1], args[2], args[3], args[4], args[5])
533 case 'A':
534 e.AbsArcTo(args[0], args[1], args[2], args[3] != 0, args[4] != 0, args[5], args[6])
535 case 'a':
536 e.RelArcTo(args[0], args[1], args[2], args[3] != 0, args[4] != 0, args[5], args[6])
537 case 'z':
538 // No-op.
539 default:
540 panic("unrecognized verb")
541 }
542 }
543 e.ClosePathEndPath()
544 return nil
545 }
546
547 func TestEncodeGradient(t *testing.T) {
548 rgb := []GradientStop{
549 {Offset: 0.00, Color: color.RGBA{0xff, 0x00, 0x00, 0xff}},
550 {Offset: 0.25, Color: color.RGBA{0x00, 0xff, 0x00, 0xff}},
551 {Offset: 0.50, Color: color.RGBA{0x00, 0x00, 0xff, 0xff}},
552 {Offset: 1.00, Color: color.RGBA{0x00, 0x00, 0x00, 0xff}},
553 }
554 cmy := []GradientStop{
555 {Offset: 0.00, Color: color.RGBA{0x00, 0xff, 0xff, 0xff}},
556 {Offset: 0.25, Color: color.RGBA{0xff, 0xff, 0xff, 0xff}},
557 {Offset: 0.50, Color: color.RGBA{0xff, 0x00, 0xff, 0xff}},
558 {Offset: 0.75, Color: color.RGBA{0x00, 0x00, 0x00, 0x00}},
559 {Offset: 1.00, Color: color.RGBA{0xff, 0xff, 0x00, 0xff}},
560 }
561
562 var e Encoder
563
564 e.SetLinearGradient(10, 10, -12, -30, +12, -18, GradientSpreadNone, rgb)
565 e.StartPath(0, -30, -30)
566 e.AbsHLineTo(+30)
567 e.AbsVLineTo(-18)
568 e.AbsHLineTo(-30)
569 e.ClosePathEndPath()
570
571 e.SetLinearGradient(10, 10, -12, -14, +12, -2, GradientSpreadPad, cmy)
572 e.StartPath(0, -30, -14)
573 e.AbsHLineTo(+30)
574 e.AbsVLineTo(-2)
575 e.AbsHLineTo(-30)
576 e.ClosePathEndPath()
577
578 e.SetCircularGradient(10, 10, -8, 8, 0, 16, GradientSpreadReflect, rgb)
579 e.StartPath(0, -30, +2)
580 e.AbsHLineTo(+30)
581 e.AbsVLineTo(+14)
582 e.AbsHLineTo(-30)
583 e.ClosePathEndPath()
584
585 e.SetCircularGradient(10, 10, -8, 24, 0, 16, GradientSpreadRepeat, cmy)
586 e.StartPath(0, -30, +18)
587 e.AbsHLineTo(+30)
588 e.AbsVLineTo(+30)
589 e.AbsHLineTo(-30)
590 e.ClosePathEndPath()
591
592 testEncode(t, &e, "testdata/gradient.ivg")
593 }
594
595 func TestEncodeLODPolygon(t *testing.T) {
596 var e Encoder
597
598 poly := func(n int) {
599 const r = 28
600 angle := 2 * math.Pi / float64(n)
601 e.StartPath(0, r, 0)
602 for i := 1; i < n; i++ {
603 e.AbsLineTo(
604 float32(r*math.Cos(angle*float64(i))),
605 float32(r*math.Sin(angle*float64(i))),
606 )
607 }
608 e.ClosePathEndPath()
609 }
610
611 e.StartPath(0, -28, -20)
612 e.AbsVLineTo(-28)
613 e.AbsHLineTo(-20)
614 e.ClosePathEndPath()
615
616 e.SetLOD(0, 80)
617 poly(3)
618
619 e.SetLOD(80, positiveInfinity)
620 poly(5)
621
622 e.SetLOD(0, positiveInfinity)
623 e.StartPath(0, +28, +20)
624 e.AbsVLineTo(+28)
625 e.AbsHLineTo(+20)
626 e.ClosePathEndPath()
627
628 testEncode(t, &e, "testdata/lod-polygon.ivg")
629 }
630
631 var video005PrimitiveSVGData = []struct {
632 r, g, b uint32
633 x0, y0 int
634 x1, y1 int
635 x2, y2 int
636 }{
637 {0x17, 0x06, 0x05, 162, 207, 271, 186, 195, -16},
638 {0xe9, 0xf5, 0xf8, -16, 179, 140, -11, 16, -8},
639 {0x00, 0x04, 0x27, 97, 96, 221, 21, 214, 111},
640 {0x89, 0xd9, 0xff, 262, -6, 271, 104, 164, -16},
641 {0x94, 0xbd, 0xc5, 204, 104, 164, 207, 59, 104},
642 {0xd4, 0x81, 0x3d, -16, 36, 123, 195, -16, 194},
643 {0x00, 0x00, 0x00, 164, 19, 95, 77, 138, 13},
644 {0x39, 0x11, 0x19, 50, 143, 115, 185, -4, 165},
645 {0x00, 0x3d, 0x81, 86, 109, 53, 76, 90, 24},
646 {0xfc, 0xc6, 0x9c, 31, 161, 80, 105, -16, 28},
647 {0x9e, 0xdd, 0xff, 201, -7, 31, -16, 2, 60},
648 {0x01, 0x20, 0x39, 132, 85, 240, -5, 173, 130},
649 {0xfd, 0xbc, 0x8f, 193, 127, 231, 94, 250, 124},
650 {0x43, 0x06, 0x00, 251, 207, 237, 83, 271, 97},
651 {0x80, 0xbf, 0xee, 117, 134, 88, 177, 90, 28},
652 {0x00, 0x00, 0x00, 127, 38, 172, 68, 223, 55},
653 {0x19, 0x0e, 0x16, 201, 204, 161, 101, 271, 192},
654 {0xf6, 0xaa, 0x71, 201, 164, 226, 141, 261, 152},
655 {0xe0, 0x36, 0x00, -16, -2, 29, -16, -6, 58},
656 {0xff, 0xe4, 0xba, 146, 45, 118, 75, 148, 76},
657 {0x00, 0x00, 0x12, 118, 44, 107, 109, 100, 51},
658 {0xbd, 0xd5, 0xe4, 271, 41, 253, -16, 211, 89},
659 {0x52, 0x00, 0x00, 87, 127, 83, 150, 55, 111},
660 {0x00, 0xb3, 0xa1, 124, 185, 135, 207, 194, 176},
661 {0x22, 0x00, 0x00, 59, 151, 33, 124, 52, 169},
662 {0xbe, 0xcb, 0xcb, 149, 42, 183, -16, 178, 47},
663 {0xff, 0xd4, 0xb1, 211, 119, 184, 100, 182, 124},
664 {0xff, 0xe1, 0x39, 73, 207, 140, 180, -13, 187},
665 {0xa7, 0xb0, 0xad, 122, 181, 200, 182, 93, 82},
666 {0x00, 0x00, 0x00, 271, 168, 170, 185, 221, 207},
667 }
668
669 func TestEncodeVideo005Primitive(t *testing.T) {
670 // The division by 4 is because the SVG width is 256 units and the IconVG
671 // width is 64 (from -32 to +32).
672 //
673 // The subtraction by 0.5 is because the SVG file contains the line:
674 // <g transform="translate(0.5 0.5)">
675 scaleX := func(i int) float32 { return float32(i)/4 - (32 - 0.5/4) }
676 scaleY := func(i int) float32 { return float32(i)/4 - (24 - 0.5/4) }
677
678 var e Encoder
679 e.Reset(Metadata{
680 ViewBox: Rectangle{
681 Min: f32.Vec2{-32, -24},
682 Max: f32.Vec2{+32, +24},
683 },
684 Palette: DefaultPalette,
685 })
686
687 e.SetCReg(0, false, RGBAColor(color.RGBA{0x7c, 0x7e, 0x7c, 0xff}))
688 e.StartPath(0, -32, -24)
689 e.AbsHLineTo(+32)
690 e.AbsVLineTo(+24)
691 e.AbsHLineTo(-32)
692 e.ClosePathEndPath()
693
694 for _, v := range video005PrimitiveSVGData {
695 e.SetCReg(0, false, RGBAColor(color.RGBA{
696 uint8(v.r * 128 / 255),
697 uint8(v.g * 128 / 255),
698 uint8(v.b * 128 / 255),
699 128,
700 }))
701 e.StartPath(0, scaleX(v.x0), scaleY(v.y0))
702 e.AbsLineTo(scaleX(v.x1), scaleY(v.y1))
703 e.AbsLineTo(scaleX(v.x2), scaleY(v.y2))
704 e.ClosePathEndPath()
705 }
706
707 testEncode(t, &e, "testdata/video-005.primitive.ivg")
708 }