-
Notifications
You must be signed in to change notification settings - Fork 0
/
clip.go
348 lines (310 loc) · 8.17 KB
/
clip.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
// SPDX-License-Identifier: Unlicense OR MIT
package clip
import (
"encoding/binary"
"hash/maphash"
"image"
"math"
"github.com/utopiagio/gio/f32"
f32internal "github.com/utopiagio/gio/internal/f32"
"github.com/utopiagio/gio/internal/ops"
"github.com/utopiagio/gio/internal/scene"
"github.com/utopiagio/gio/internal/stroke"
"github.com/utopiagio/gio/op"
)
// Op represents a clip area. Op intersects the current clip area with
// itself.
type Op struct {
path PathSpec
outline bool
width float32
}
// Stack represents an Op pushed on the clip stack.
type Stack struct {
ops *ops.Ops
id ops.StackID
macroID uint32
}
var pathSeed maphash.Seed
func init() {
pathSeed = maphash.MakeSeed()
}
// Push saves the current clip state on the stack and updates the current
// state to the intersection of the current p.
func (p Op) Push(o *op.Ops) Stack {
id, macroID := ops.PushOp(&o.Internal, ops.ClipStack)
p.add(o)
return Stack{ops: &o.Internal, id: id, macroID: macroID}
}
func (p Op) add(o *op.Ops) {
path := p.path
if !path.hasSegments && p.width > 0 {
switch p.path.shape {
case ops.Rect:
b := f32internal.FRect(path.bounds)
var rect Path
rect.Begin(o)
rect.MoveTo(b.Min)
rect.LineTo(f32.Pt(b.Max.X, b.Min.Y))
rect.LineTo(b.Max)
rect.LineTo(f32.Pt(b.Min.X, b.Max.Y))
rect.Close()
path = rect.End()
case ops.Path:
// Nothing to do.
default:
panic("invalid empty path for shape")
}
}
bo := binary.LittleEndian
if path.hasSegments {
data := ops.Write(&o.Internal, ops.TypePathLen)
data[0] = byte(ops.TypePath)
bo.PutUint64(data[1:], path.hash)
path.spec.Add(o)
}
bounds := path.bounds
if p.width > 0 {
// Expand bounds to cover stroke.
half := int(p.width*.5 + .5)
bounds.Min.X -= half
bounds.Min.Y -= half
bounds.Max.X += half
bounds.Max.Y += half
data := ops.Write(&o.Internal, ops.TypeStrokeLen)
data[0] = byte(ops.TypeStroke)
bo := binary.LittleEndian
bo.PutUint32(data[1:], math.Float32bits(p.width))
}
data := ops.Write(&o.Internal, ops.TypeClipLen)
data[0] = byte(ops.TypeClip)
bo.PutUint32(data[1:], uint32(bounds.Min.X))
bo.PutUint32(data[5:], uint32(bounds.Min.Y))
bo.PutUint32(data[9:], uint32(bounds.Max.X))
bo.PutUint32(data[13:], uint32(bounds.Max.Y))
if p.outline {
data[17] = byte(1)
}
data[18] = byte(path.shape)
}
func (s Stack) Pop() {
ops.PopOp(s.ops, ops.ClipStack, s.id, s.macroID)
data := ops.Write(s.ops, ops.TypePopClipLen)
data[0] = byte(ops.TypePopClip)
}
type PathSpec struct {
spec op.CallOp
// hasSegments tracks whether there are any segments in the path.
hasSegments bool
bounds image.Rectangle
shape ops.Shape
hash uint64
}
// Path constructs a Op clip path described by lines and
// Bézier curves, where drawing outside the Path is discarded.
// The inside-ness of a pixel is determines by the non-zero winding rule,
// similar to the SVG rule of the same name.
//
// Path generates no garbage and can be used for dynamic paths; path
// data is stored directly in the Ops list supplied to Begin.
type Path struct {
ops *ops.Ops
contour int
pen f32.Point
macro op.MacroOp
start f32.Point
hasSegments bool
bounds f32internal.Rectangle
hash maphash.Hash
}
// Pos returns the current pen position.
func (p *Path) Pos() f32.Point { return p.pen }
// Begin the path, storing the path data and final Op into ops.
func (p *Path) Begin(o *op.Ops) {
*p = Path{
ops: &o.Internal,
macro: op.Record(o),
contour: 1,
}
p.hash.SetSeed(pathSeed)
ops.BeginMulti(p.ops)
data := ops.WriteMulti(p.ops, ops.TypeAuxLen)
data[0] = byte(ops.TypeAux)
}
// End returns a PathSpec ready to use in clipping operations.
func (p *Path) End() PathSpec {
p.gap()
c := p.macro.Stop()
ops.EndMulti(p.ops)
return PathSpec{
spec: c,
hasSegments: p.hasSegments,
bounds: p.bounds.Round(),
hash: p.hash.Sum64(),
}
}
// Move moves the pen by the amount specified by delta.
func (p *Path) Move(delta f32.Point) {
to := delta.Add(p.pen)
p.MoveTo(to)
}
// MoveTo moves the pen to the specified absolute coordinate.
func (p *Path) MoveTo(to f32.Point) {
if p.pen == to {
return
}
p.gap()
p.end()
p.pen = to
p.start = to
}
func (p *Path) gap() {
if p.pen != p.start {
// A closed contour starts and ends in the same point.
// This move creates a gap in the contour, register it.
data := ops.WriteMulti(p.ops, scene.CommandSize+4)
bo := binary.LittleEndian
bo.PutUint32(data[0:], uint32(p.contour))
p.cmd(data[4:], scene.Gap(p.pen, p.start))
}
}
// end completes the current contour.
func (p *Path) end() {
p.contour++
}
// Line moves the pen by the amount specified by delta, recording a line.
func (p *Path) Line(delta f32.Point) {
to := delta.Add(p.pen)
p.LineTo(to)
}
// LineTo moves the pen to the absolute point specified, recording a line.
func (p *Path) LineTo(to f32.Point) {
if to == p.pen {
return
}
data := ops.WriteMulti(p.ops, scene.CommandSize+4)
bo := binary.LittleEndian
bo.PutUint32(data[0:], uint32(p.contour))
p.cmd(data[4:], scene.Line(p.pen, to))
p.pen = to
p.expand(to)
}
func (p *Path) cmd(data []byte, c scene.Command) {
ops.EncodeCommand(data, c)
p.hash.Write(data)
}
func (p *Path) expand(pt f32.Point) {
if !p.hasSegments {
p.hasSegments = true
p.bounds = f32internal.Rectangle{Min: pt, Max: pt}
} else {
b := p.bounds
if pt.X < b.Min.X {
b.Min.X = pt.X
}
if pt.Y < b.Min.Y {
b.Min.Y = pt.Y
}
if pt.X > b.Max.X {
b.Max.X = pt.X
}
if pt.Y > b.Max.Y {
b.Max.Y = pt.Y
}
p.bounds = b
}
}
// Quad records a quadratic Bézier from the pen to end
// with the control point ctrl.
func (p *Path) Quad(ctrl, to f32.Point) {
ctrl = ctrl.Add(p.pen)
to = to.Add(p.pen)
p.QuadTo(ctrl, to)
}
// QuadTo records a quadratic Bézier from the pen to end
// with the control point ctrl, with absolute coordinates.
func (p *Path) QuadTo(ctrl, to f32.Point) {
if ctrl == p.pen && to == p.pen {
return
}
data := ops.WriteMulti(p.ops, scene.CommandSize+4)
bo := binary.LittleEndian
bo.PutUint32(data[0:], uint32(p.contour))
p.cmd(data[4:], scene.Quad(p.pen, ctrl, to))
p.pen = to
p.expand(ctrl)
p.expand(to)
}
// ArcTo adds an elliptical arc to the path. The implied ellipse is defined
// by its focus points f1 and f2.
// The arc starts in the current point and ends angle radians along the ellipse boundary.
// The sign of angle determines the direction; positive being counter-clockwise,
// negative clockwise.
func (p *Path) ArcTo(f1, f2 f32.Point, angle float32) {
m, segments := stroke.ArcTransform(p.pen, f1, f2, angle)
for i := 0; i < segments; i++ {
p0 := p.pen
p1 := m.Transform(p0)
p2 := m.Transform(p1)
ctl := p1.Mul(2).Sub(p0.Add(p2).Mul(.5))
p.QuadTo(ctl, p2)
}
}
// Arc is like ArcTo where f1 and f2 are relative to the current position.
func (p *Path) Arc(f1, f2 f32.Point, angle float32) {
f1 = f1.Add(p.pen)
f2 = f2.Add(p.pen)
p.ArcTo(f1, f2, angle)
}
// Cube records a cubic Bézier from the pen through
// two control points ending in to.
func (p *Path) Cube(ctrl0, ctrl1, to f32.Point) {
p.CubeTo(p.pen.Add(ctrl0), p.pen.Add(ctrl1), p.pen.Add(to))
}
// CubeTo records a cubic Bézier from the pen through
// two control points ending in to, with absolute coordinates.
func (p *Path) CubeTo(ctrl0, ctrl1, to f32.Point) {
if ctrl0 == p.pen && ctrl1 == p.pen && to == p.pen {
return
}
data := ops.WriteMulti(p.ops, scene.CommandSize+4)
bo := binary.LittleEndian
bo.PutUint32(data[0:], uint32(p.contour))
p.cmd(data[4:], scene.Cubic(p.pen, ctrl0, ctrl1, to))
p.pen = to
p.expand(ctrl0)
p.expand(ctrl1)
p.expand(to)
}
// Close closes the path contour.
func (p *Path) Close() {
if p.pen != p.start {
p.LineTo(p.start)
}
p.end()
}
// Stroke represents a stroked path.
type Stroke struct {
Path PathSpec
// Width of the stroked path.
Width float32
}
// Op returns a clip operation representing the stroke.
func (s Stroke) Op() Op {
return Op{
path: s.Path,
width: s.Width,
}
}
// Outline represents the area inside of a path, according to the
// non-zero winding rule.
type Outline struct {
Path PathSpec
}
// Op returns a clip operation representing the outline.
func (o Outline) Op() Op {
return Op{
path: o.Path,
outline: true,
}
}