-
Notifications
You must be signed in to change notification settings - Fork 40
/
paths.go
531 lines (452 loc) · 14.4 KB
/
paths.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package canvas
import (
"math"
"github.com/tfriedel6/canvas/backend/backendbase"
)
// BeginPath clears the current path and starts a new one
func (cv *Canvas) BeginPath() {
if cv.path.p == nil {
cv.path.p = make([]pathPoint, 0, 100)
}
cv.path.p = cv.path.p[:0]
}
func isSamePoint(a, b backendbase.Vec, maxDist float64) bool {
return math.Abs(b[0]-a[0]) <= maxDist && math.Abs(b[1]-a[1]) <= maxDist
}
// MoveTo adds a gap and moves the end of the path to x/y
func (cv *Canvas) MoveTo(x, y float64) {
tf := cv.tf(backendbase.Vec{x, y})
cv.path.MoveTo(tf[0], tf[1])
}
// LineTo adds a line to the end of the path
func (cv *Canvas) LineTo(x, y float64) {
tf := cv.tf(backendbase.Vec{x, y})
cv.path.LineTo(tf[0], tf[1])
}
// Arc adds a circle segment to the end of the path. x/y is the center, radius
// is the radius, startAngle and endAngle are angles in radians, anticlockwise
// means that the line is added anticlockwise
func (cv *Canvas) Arc(x, y, radius, startAngle, endAngle float64, anticlockwise bool) {
ax, ay := math.Sincos(startAngle)
startAngle2 := backendbase.Vec{ay, ax}.MulMat2(cv.state.transform.Mat2()).Atan2()
endAngle2 := startAngle2 + (endAngle - startAngle)
cv.path.arc(x, y, radius, startAngle2, endAngle2, anticlockwise, cv.state.transform, false)
}
// ArcTo adds to the current path by drawing a line toward x1/y1 and a circle
// segment of a radius given by the radius parameter. The circle touches the
// lines from the end of the path to x1/y1, and from x1/y1 to x2/y2. The line
// will only go to where the circle segment would touch the latter line
func (cv *Canvas) ArcTo(x1, y1, x2, y2, radius float64) {
cv.path.arcTo(x1, y1, x2, y2, radius, cv.state.transform, false)
}
// QuadraticCurveTo adds a quadratic curve to the path. It uses the current end
// point of the path, x1/y1 defines the curve, and x2/y2 is the end point
func (cv *Canvas) QuadraticCurveTo(x1, y1, x2, y2 float64) {
tf1 := cv.tf(backendbase.Vec{x1, y1})
tf2 := cv.tf(backendbase.Vec{x2, y2})
cv.path.QuadraticCurveTo(tf1[0], tf1[1], tf2[0], tf2[1])
}
// BezierCurveTo adds a bezier curve to the path. It uses the current end point
// of the path, x1/y1 and x2/y2 define the curve, and x3/y3 is the end point
func (cv *Canvas) BezierCurveTo(x1, y1, x2, y2, x3, y3 float64) {
tf1 := cv.tf(backendbase.Vec{x1, y1})
tf2 := cv.tf(backendbase.Vec{x2, y2})
tf3 := cv.tf(backendbase.Vec{x3, y3})
cv.path.BezierCurveTo(tf1[0], tf1[1], tf2[0], tf2[1], tf3[0], tf3[1])
}
// Ellipse adds an ellipse segment to the end of the path. x/y is the center,
// radiusX is the major axis radius, radiusY is the minor axis radius,
// rotation is the rotation of the ellipse in radians, startAngle and endAngle
// are angles in radians, and anticlockwise means that the line is added
// anticlockwise
func (cv *Canvas) Ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle float64, anticlockwise bool) {
tf := cv.tf(backendbase.Vec{x, y})
ax, ay := math.Sincos(startAngle)
startAngle2 := backendbase.Vec{ay, ax}.MulMat2(cv.state.transform.Mat2()).Atan2()
endAngle2 := startAngle2 + (endAngle - startAngle)
cv.path.Ellipse(tf[0], tf[1], radiusX, radiusY, rotation, startAngle2, endAngle2, anticlockwise)
}
// ClosePath closes the path to the beginning of the path or the last point
// from a MoveTo call
func (cv *Canvas) ClosePath() {
cv.path.ClosePath()
}
// Stroke uses the current StrokeStyle to draw the current path
func (cv *Canvas) Stroke() {
cv.strokePath(&cv.path, cv.state.transform, cv.state.transform.Invert(), true)
}
// StrokePath uses the current StrokeStyle to draw the given path
func (cv *Canvas) StrokePath(path *Path2D) {
// todo avoid allocation
path2 := Path2D{
p: make([]pathPoint, len(path.p)),
}
copy(path2.p, path.p)
cv.strokePath(&path2, cv.state.transform, backendbase.Mat{}, false)
}
func (cv *Canvas) strokePath(path *Path2D, tf backendbase.Mat, inv backendbase.Mat, doInv bool) {
if len(path.p) == 0 {
return
}
var triBuf [500]backendbase.Vec
tris := cv.strokeTris(path, tf, inv, doInv, triBuf[:0])
cv.drawShadow(tris, nil, true)
stl := cv.backendFillStyle(&cv.state.stroke, 1)
cv.b.Fill(&stl, tris, backendbase.MatIdentity, true)
}
func (cv *Canvas) strokeTris(path *Path2D, tf backendbase.Mat, inv backendbase.Mat, doInv bool, target []backendbase.Vec) []backendbase.Vec {
if len(path.p) == 0 {
return target
}
if doInv {
pcopy := *path
var pbuf [50]pathPoint
if len(path.p) <= len(pbuf) {
pcopy.p = pbuf[:len(path.p)]
} else {
pcopy.p = make([]pathPoint, len(path.p))
}
for i, pt := range path.p {
pt.pos = pt.pos.MulMat(inv)
pt.next = pt.next.MulMat(inv)
pcopy.p[i] = pt
}
path = &pcopy
}
dashedPath := cv.applyLineDash(path.p)
start := true
var p0 backendbase.Vec
for _, p := range dashedPath {
if p.flags&pathMove != 0 {
p0 = p.pos
start = true
continue
}
p1 := p.pos
v0 := p1.Sub(p0).Norm()
v1 := backendbase.Vec{v0[1], -v0[0]}.Mulf(cv.state.lineWidth * 0.5)
v0 = v0.Mulf(cv.state.lineWidth * 0.5)
lp0 := p0.Add(v1)
lp1 := p1.Add(v1)
lp2 := p0.Sub(v1)
lp3 := p1.Sub(v1)
if start {
switch cv.state.lineCap {
case Butt:
// no need to do anything
case Square:
lp0 = lp0.Sub(v0)
lp2 = lp2.Sub(v0)
case Round:
target = cv.addCircleTris(p0, cv.state.lineWidth*0.5, tf, target)
}
}
if p.flags&pathAttach == 0 {
switch cv.state.lineCap {
case Butt:
// no need to do anything
case Square:
lp1 = lp1.Add(v0)
lp3 = lp3.Add(v0)
case Round:
target = cv.addCircleTris(p1, cv.state.lineWidth*0.5, tf, target)
}
}
target = append(target, lp0.MulMat(tf), lp1.MulMat(tf), lp3.MulMat(tf), lp0.MulMat(tf), lp3.MulMat(tf), lp2.MulMat(tf))
if p.flags&pathAttach != 0 && cv.state.lineWidth > 1 {
target = cv.lineJoint(p0, p1, p.next, lp0, lp1, lp2, lp3, tf, target)
}
p0 = p1
start = false
}
return target
}
func (cv *Canvas) applyLineDash(path []pathPoint) []pathPoint {
if len(cv.state.lineDash) < 2 || len(path) < 2 {
return path
}
ldo := cv.state.lineDashOffset
ldp := cv.state.lineDashPoint
path2 := make([]pathPoint, 0, len(path)*2)
var lp pathPoint
for i, pp := range path {
if i == 0 || pp.flags&pathMove != 0 {
path2 = append(path2, pp)
lp = pp
continue
}
v := pp.pos.Sub(lp.pos)
vl := v.Len()
prev := ldo
for vl > 0 {
draw := ldp%2 == 0
newp := pathPoint{pos: pp.pos}
ldo += vl
if ldo > cv.state.lineDash[ldp] {
ldo = 0
dl := cv.state.lineDash[ldp] - prev
dist := dl / vl
newp.pos = lp.pos.Add(v.Mulf(dist))
vl -= dl
ldp++
ldp %= len(cv.state.lineDash)
prev = 0
} else {
vl = 0
}
if draw {
path2[len(path2)-1].next = newp.pos
path2[len(path2)-1].flags |= pathAttach
path2 = append(path2, newp)
} else {
newp.flags = pathMove
path2 = append(path2, newp)
}
lp = newp
v = pp.pos.Sub(lp.pos)
}
lp = pp
}
return path2
}
func (cv *Canvas) lineJoint(p0, p1, p2, l0p0, l0p1, l0p2, l0p3 backendbase.Vec, tf backendbase.Mat, tris []backendbase.Vec) []backendbase.Vec {
v2 := p1.Sub(p2).Norm()
v3 := backendbase.Vec{v2[1], -v2[0]}.Mulf(cv.state.lineWidth * 0.5)
switch cv.state.lineJoin {
case Miter:
l1p0 := p2.Sub(v3)
l1p1 := p1.Sub(v3)
l1p2 := p2.Add(v3)
l1p3 := p1.Add(v3)
var ip0, ip1 backendbase.Vec
if l0p1.Sub(l1p1).LenSqr() < 0.000000001 {
ip0 = l0p1.Sub(l1p1).Mulf(0.5).Add(l1p1)
} else {
var q float64
ip0, _, q = lineIntersection(l0p0, l0p1, l1p1, l1p0)
if q >= 1 {
ip0 = l0p1.Add(l1p1).Mulf(0.5)
}
}
if dist := ip0.Sub(l0p1).LenSqr(); dist > cv.state.miterLimitSqr {
l1p1 := p1.Sub(v3)
l1p3 := p1.Add(v3)
tris = append(tris, p1.MulMat(tf), l0p1.MulMat(tf), l1p1.MulMat(tf),
p1.MulMat(tf), l1p3.MulMat(tf), l0p3.MulMat(tf))
return tris
}
if l0p3.Sub(l1p3).LenSqr() < 0.000000001 {
ip1 = l0p3.Sub(l1p3).Mulf(0.5).Add(l1p3)
} else {
var q float64
ip1, _, q = lineIntersection(l0p2, l0p3, l1p3, l1p2)
if q >= 1 {
ip1 = l0p3.Add(l1p3).Mulf(0.5)
}
}
if dist := ip1.Sub(l1p1).LenSqr(); dist > cv.state.miterLimitSqr {
l1p1 := p1.Sub(v3)
l1p3 := p1.Add(v3)
tris = append(tris, p1.MulMat(tf), l0p1.MulMat(tf), l1p1.MulMat(tf),
p1.MulMat(tf), l1p3.MulMat(tf), l0p3.MulMat(tf))
return tris
}
tris = append(tris, p1.MulMat(tf), l0p1.MulMat(tf), ip0.MulMat(tf),
p1.MulMat(tf), ip0.MulMat(tf), l1p1.MulMat(tf),
p1.MulMat(tf), l1p3.MulMat(tf), ip1.MulMat(tf),
p1.MulMat(tf), ip1.MulMat(tf), l0p3.MulMat(tf))
case Bevel:
l1p1 := p1.Sub(v3)
l1p3 := p1.Add(v3)
tris = append(tris, p1.MulMat(tf), l0p1.MulMat(tf), l1p1.MulMat(tf),
p1.MulMat(tf), l1p3.MulMat(tf), l0p3.MulMat(tf))
case Round:
tris = cv.addCircleTris(p1, cv.state.lineWidth*0.5, tf, tris)
}
return tris
}
func (cv *Canvas) addCircleTris(center backendbase.Vec, radius float64, tf backendbase.Mat, tris []backendbase.Vec) []backendbase.Vec {
step := 6 / radius
if step > 0.8 {
step = 0.8
} else if step < 0.05 {
step = 0.05
}
centertf := center.MulMat(tf)
p0 := backendbase.Vec{center[0], center[1] + radius}.MulMat(tf)
for angle := step; angle <= math.Pi*2+step; angle += step {
s, c := math.Sincos(angle)
p1 := backendbase.Vec{center[0] + s*radius, center[1] + c*radius}.MulMat(tf)
tris = append(tris, centertf, p0, p1)
p0 = p1
}
return tris
}
func lineIntersection(a0, a1, b0, b1 backendbase.Vec) (backendbase.Vec, float64, float64) {
va := a1.Sub(a0)
vb := b1.Sub(b0)
if (va[0] == 0 && vb[0] == 0) || (va[1] == 0 && vb[1] == 0) || (va[0] == 0 && va[1] == 0) || (vb[0] == 0 && vb[1] == 0) {
return backendbase.Vec{}, float64(math.Inf(1)), float64(math.Inf(1))
}
d := va[1]*vb[0] - va[0]*vb[1]
if d == 0 {
return backendbase.Vec{}, float64(math.Inf(1)), float64(math.Inf(1))
}
p := (vb[1]*(a0[0]-b0[0]) - a0[1]*vb[0] + b0[1]*vb[0]) / d
var q float64
if vb[0] == 0 {
q = (a0[1] + p*va[1] - b0[1]) / vb[1]
} else {
q = (a0[0] + p*va[0] - b0[0]) / vb[0]
}
return a0.Add(va.Mulf(p)), p, q
}
func linePointDistSqr(a, b, p backendbase.Vec) float64 {
v := b.Sub(a)
vl := v.Len()
vn := v.Divf(vl)
d := p.Sub(a).Dot(vn)
c := a.Add(vn.Mulf(d))
return p.Sub(c).LenSqr()
}
// Fill fills the current path with the current FillStyle
func (cv *Canvas) Fill() {
cv.fillPath(&cv.path, backendbase.MatIdentity)
}
// FillPath fills the given path with the current FillStyle
func (cv *Canvas) FillPath(path *Path2D) {
cv.fillPath(path, cv.state.transform)
}
// FillPath fills the given path with the current FillStyle
func (cv *Canvas) fillPath(path *Path2D, tf backendbase.Mat) {
if len(path.p) < 3 {
return
}
var tris []backendbase.Vec
var triBuf [500]backendbase.Vec
if path.standalone && path.fillCache != nil {
tris = path.fillCache
} else {
if path.standalone {
tris = make([]backendbase.Vec, 0, 500)
} else {
tris = triBuf[:0]
}
runSubPaths(path.p, true, func(sp []pathPoint) bool {
tris = appendSubPathTriangles(tris, backendbase.MatIdentity, sp)
return false
})
if path.standalone {
path.fillCache = tris
}
}
if len(tris) == 0 {
return
}
cv.drawShadow(tris, nil, false)
stl := cv.backendFillStyle(&cv.state.fill, 1)
cv.b.Fill(&stl, tris, tf, false)
}
func appendSubPathTriangles(tris []backendbase.Vec, mat backendbase.Mat, path []pathPoint) []backendbase.Vec {
last := path[len(path)-1]
if last.flags&pathIsConvex != 0 {
p0, p1 := path[0].pos.MulMat(mat), path[1].pos.MulMat(mat)
lastIdx := len(path)
if path[0].pos == path[lastIdx-1].pos {
lastIdx--
}
for i := 2; i < lastIdx; i++ {
p2 := path[i].pos.MulMat(mat)
tris = append(tris, p0, p1, p2)
p1 = p2
}
} else if last.flags&pathSelfIntersects != 0 {
selfIntersectingPathParts(path, func(sp []pathPoint) bool {
tris = triangulatePath(sp, mat, tris)
return false
})
} else {
tris = triangulatePath(path, mat, tris)
}
return tris
}
// Clip uses the current path to clip any further drawing. Use Save/Restore to
// remove the clipping again
func (cv *Canvas) Clip() {
cv.clip(&cv.path, backendbase.MatIdentity)
}
func (cv *Canvas) clip(path *Path2D, tf backendbase.Mat) {
if len(path.p) < 3 {
return
}
var buf [500]backendbase.Vec
if path.p[len(path.p)-1].flags&pathIsRect != 0 {
cv.state.clip.p = make([]pathPoint, len(path.p))
copy(cv.state.clip.p, path.p)
quad := buf[:4]
for i := range quad {
quad[i] = path.p[i].pos
}
cv.b.Clip(quad)
return
}
tris := buf[:0]
runSubPaths(path.p, true, func(sp []pathPoint) bool {
tris = appendSubPathTriangles(tris, tf, sp)
return false
})
if len(tris) == 0 {
return
}
cv.state.clip.p = make([]pathPoint, len(path.p))
copy(cv.state.clip.p, path.p)
cv.b.Clip(tris)
}
// Rect creates a closed rectangle path for stroking or filling
func (cv *Canvas) Rect(x, y, w, h float64) {
lastWasMove := len(cv.path.p) == 0 || cv.path.p[len(cv.path.p)-1].flags&pathMove != 0
cv.MoveTo(x, y)
cv.LineTo(x+w, y)
cv.LineTo(x+w, y+h)
cv.LineTo(x, y+h)
cv.LineTo(x, y)
if lastWasMove {
cv.path.p[len(cv.path.p)-1].flags |= pathIsRect
cv.path.p[len(cv.path.p)-1].flags |= pathIsConvex
}
}
// StrokeRect draws a rectangle using the current stroke style
func (cv *Canvas) StrokeRect(x, y, w, h float64) {
v0 := backendbase.Vec{x, y}
v1 := backendbase.Vec{x + w, y}
v2 := backendbase.Vec{x + w, y + h}
v3 := backendbase.Vec{x, y + h}
var p [5]pathPoint
p[0] = pathPoint{pos: v0, flags: pathMove | pathAttach, next: v1}
p[1] = pathPoint{pos: v1, next: v2, flags: pathAttach}
p[2] = pathPoint{pos: v2, next: v3, flags: pathAttach}
p[3] = pathPoint{pos: v3, next: v0, flags: pathAttach}
p[4] = pathPoint{pos: v0, next: v1, flags: pathAttach}
path := Path2D{p: p[:]}
cv.strokePath(&path, cv.state.transform, backendbase.Mat{}, false)
}
// FillRect fills a rectangle with the active fill style
func (cv *Canvas) FillRect(x, y, w, h float64) {
p0 := cv.tf(backendbase.Vec{x, y})
p1 := cv.tf(backendbase.Vec{x, y + h})
p2 := cv.tf(backendbase.Vec{x + w, y + h})
p3 := cv.tf(backendbase.Vec{x + w, y})
data := [4]backendbase.Vec{{p0[0], p0[1]}, {p1[0], p1[1]}, {p2[0], p2[1]}, {p3[0], p3[1]}}
cv.drawShadow(data[:], nil, false)
stl := cv.backendFillStyle(&cv.state.fill, 1)
cv.b.Fill(&stl, data[:], backendbase.MatIdentity, false)
}
// ClearRect sets the color of the rectangle to transparent black
func (cv *Canvas) ClearRect(x, y, w, h float64) {
p0 := cv.tf(backendbase.Vec{x, y})
p1 := cv.tf(backendbase.Vec{x, y + h})
p2 := cv.tf(backendbase.Vec{x + w, y + h})
p3 := cv.tf(backendbase.Vec{x + w, y})
data := [4]backendbase.Vec{{p0[0], p0[1]}, {p1[0], p1[1]}, {p2[0], p2[1]}, {p3[0], p3[1]}}
cv.b.Clear(data)
}