-
Notifications
You must be signed in to change notification settings - Fork 14
/
abs.go
380 lines (340 loc) · 12.2 KB
/
abs.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
package giocanvas
import (
"image"
"image/color"
_ "image/gif" // needed by image
_ "image/jpeg"
_ "image/png"
"math"
"os"
"gioui.org/f32"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget/material"
)
// Foundational methods, and methods using Gio standard coordinates
// textops places text
func (c *Canvas) textops(x, y, size float32, alignment text.Alignment, s string, fillcolor color.NRGBA) {
offset := x
switch alignment {
case text.End:
offset = x - c.Width
case text.Middle:
offset = x - c.Width/2
}
stack := op.Offset(image.Point{X: int(offset), Y: int(y - size)}).Push(c.Context.Ops) // shift to use baseline
l := material.Label(c.Theme, unit.Sp(size), s)
l.Color = fillcolor
l.Alignment = alignment
l.Layout(c.Context)
stack.Pop()
}
// AbsTextWrap places and wraps text at (x, y), wrapped at width
func (c *Canvas) AbsTextWrap(x, y, size, width float32, s string, fillcolor color.NRGBA) {
stack := op.Offset(image.Point{X: int(x), Y: int(y - size)}).Push(c.Context.Ops) // shift to use baseline
l := material.Label(c.Theme, unit.Sp(size), s)
l.Color = fillcolor
c.Context.Constraints.Max.X = int(width)
l.Layout(c.Context)
c.Context.Constraints.Max.X = int(c.Width) // restore width...
stack.Pop()
}
// AbsText places text at (x,y)
func (c *Canvas) AbsText(x, y, size float32, s string, fillcolor color.NRGBA) {
c.textops(x, y, size, text.Start, s, fillcolor)
}
// AbsTextMid places text centered at (x,y)
func (c *Canvas) AbsTextMid(x, y, size float32, s string, fillcolor color.NRGBA) {
c.textops(x, y, size, text.Middle, s, fillcolor)
}
// AbsTextEnd places text aligned to the end
func (c *Canvas) AbsTextEnd(x, y, size float32, s string, fillcolor color.NRGBA) {
c.textops(x, y, size, text.End, s, fillcolor)
}
// AbsRect makes a filled Rectangle; left corner at (x, y), with dimensions (w,h)
func (c *Canvas) AbsRect(x, y, w, h float32, fillcolor color.NRGBA) {
px := make([]float32, 4)
py := make([]float32, 4)
px[0], py[0] = x, y
px[1], py[1] = x+w, y
px[2], py[2] = x+w, y+h
px[3], py[3] = x, y+h
c.AbsPolygon(px, py, fillcolor)
}
// AbsCenterRect makes a filled rectangle centered at (x, y), with dimensions (w,h)
func (c *Canvas) AbsCenterRect(x, y, w, h float32, fillcolor color.NRGBA) {
c.AbsRect(x-(w/2), y-(h/2), w, h, fillcolor)
}
// AbsVLine makes a vertical line beginning at (x,y) with dimension (w, h)
func (c *Canvas) AbsVLine(x, y, w, h float32, fillcolor color.NRGBA) {
c.AbsLine(x, y, x, y+h, w, fillcolor)
}
// AbsHLine makes a horizontal line starting at (x, y), with dimensions (w, h)
func (c *Canvas) AbsHLine(x, y, w, h float32, fillcolor color.NRGBA) {
c.AbsLine(x, y, x+w, y, h, fillcolor)
}
// AbsGrid uses horizontal and vertical lines to make a grid
func (c *Canvas) AbsGrid(width, height, size, interval float32, fillcolor color.NRGBA) {
var x, y float32
for y = 0; y <= height; y += height / interval {
c.AbsHLine(0, y, width, size, fillcolor)
}
for x = 0; x <= width; x += width / interval {
c.AbsVLine(x, 0, size, height, fillcolor)
}
}
// AbsCenterImage places a named image centered at (x, y)
// using the specified dimensions (w, h), and hen scaled
func (c *Canvas) AbsCenterImage(name string, x, y float32, w, h int, scale float32) {
r, err := os.Open(name)
if err != nil {
return
}
defer r.Close()
im, _, err := image.Decode(r)
if err != nil {
return
}
c.AbsImg(im, x, y, w, h, scale)
}
// AbsImg places a image.Image centered at (x, y)
// using the specified dimensions (w, h), and then scaled
func (c *Canvas) AbsImg(im image.Image, x, y float32, w, h int, scale float32) {
if im == nil {
return
}
// compute scaled image dimensions
// if w and h are zero, use the natural dimensions
sc := scale / 100
imw := float32(w) * sc
imh := float32(h) * sc
if w == 0 && h == 0 {
b := im.Bounds()
imw = float32(b.Max.X) * sc
imh = float32(b.Max.Y) * sc
}
// center the image
x = x - (imw / 2)
y = y - (imh / 2)
ops := c.Context.Ops
ix := int(x)
iy := int(y)
stack := op.Offset(image.Pt(ix, iy)).Push(ops)
op.Affine(f32.Affine2D{}.Scale(f32.Pt(0, 0), f32.Pt(sc, sc))).Add(ops)
paint.NewImageOp(im).Add(ops)
paint.PaintOp{}.Add(ops)
stack.Pop()
}
// AbsPolygon makes a closed, filled polygon with vertices in x and y
func (c *Canvas) AbsPolygon(x, y []float32, fillcolor color.NRGBA) {
if len(x) != len(y) {
return
}
path := new(clip.Path)
ops := c.Context.Ops
path.Begin(ops)
path.Move(f32.Point{X: x[0], Y: y[0]})
l := len(x)
point := f32.Point{}
for i := 1; i < l; i++ {
path.Line(f32.Point{X: x[i] - x[i-1], Y: y[i] - y[i-1]})
}
path.Line(f32.Point{X: x[0] - x[l-1], Y: y[0] - y[l-1]})
path.Line(point)
path.Close()
stack := clip.Outline{Path: path.End()}.Op().Push(ops)
paint.ColorOp{Color: fillcolor}.Add(ops)
paint.PaintOp{}.Add(ops)
stack.Pop()
}
// AbsLine makes a line from (x0,y0) to (x1, y1) using absolute coordinates
func (c *Canvas) AbsLine(x0, y0, x1, y1, size float32, fillcolor color.NRGBA) {
path := new(clip.Path)
ops := c.Context.Ops
path.Begin(ops)
path.MoveTo(f32.Point{X: x0, Y: y0})
path.LineTo(f32.Point{X: x1, Y: y1})
stack := clip.Stroke{Path: path.End(), Width: size}.Op().Push(ops)
paint.Fill(ops, fillcolor)
stack.Pop()
}
// AbsQuadBezier makes a filled quadratic curve
// starting at (x, y), control point at (cx, cy), end point (ex, ey)
func (c *Canvas) AbsQuadBezier(x, y, cx, cy, ex, ey, size float32, fillcolor color.NRGBA) {
path := new(clip.Path)
ops := c.Context.Ops
// control and endpoints are relative to the starting point
ctrl := f32.Point{X: cx - x, Y: cy - y}
to := f32.Point{X: ex - x, Y: ey - y}
path.Begin(ops)
path.Move(f32.Point{X: x, Y: y})
path.Quad(ctrl, to)
path.Close()
stack := clip.Outline{Path: path.End()}.Op().Push(ops)
paint.ColorOp{Color: fillcolor}.Add(ops)
paint.PaintOp{}.Add(ops)
stack.Pop()
}
// AbsStrokedQuadBezier makes a stroked quadratic curve
// starting at (x, y), control point at (cx, cy), end point (ex, ey)
func (c *Canvas) AbsStrokedQuadBezier(x, y, cx, cy, ex, ey, size float32, strokecolor color.NRGBA) {
path := new(clip.Path)
ops := c.Context.Ops
// control and endpoints are relative to the starting point
ctrl := f32.Point{X: cx - x, Y: cy - y}
to := f32.Point{X: ex - x, Y: ey - y}
path.Begin(ops)
path.Move(f32.Point{X: x, Y: y})
path.Quad(ctrl, to)
stack := clip.Stroke{Path: path.End(), Width: size}.Op().Push(ops)
paint.Fill(ops, strokecolor)
stack.Pop()
}
// AbsCubicBezier makes a filled cubic bezier curve
func (c *Canvas) AbsCubicBezier(x, y, cx1, cy1, cx2, cy2, ex, ey, size float32, fillcolor color.NRGBA) {
path := new(clip.Path)
ops := c.Context.Ops
// control and end points are relative to the starting point
sp := f32.Point{X: x, Y: y}
cp0 := f32.Point{X: cx1 - x, Y: cy1 - y}
cp1 := f32.Point{X: cx2 - x, Y: cy2 - y}
ep := f32.Point{X: ex - x, Y: ey - y}
path.Begin(ops)
path.Move(sp)
path.Cube(cp0, cp1, ep)
path.Close()
stack := clip.Outline{Path: path.End()}.Op().Push(ops)
paint.ColorOp{Color: fillcolor}.Add(ops)
paint.PaintOp{}.Add(ops)
stack.Pop()
}
// AbsStrokedCubicBezier makes a stroked cubic bezier curve
func (c *Canvas) AbsStrokedCubicBezier(x, y, cx1, cy1, cx2, cy2, ex, ey, size float32, strokecolor color.NRGBA) {
path := new(clip.Path)
ops := c.Context.Ops
// control and end points are relative to the starting point
sp := f32.Point{X: x, Y: y}
cp0 := f32.Point{X: cx1 - x, Y: cy1 - y}
cp1 := f32.Point{X: cx2 - x, Y: cy2 - y}
ep := f32.Point{X: ex - x, Y: ey - y}
path.Begin(ops)
path.Move(sp)
path.Cube(cp0, cp1, ep)
stack := clip.Stroke{Path: path.End(), Width: size}.Op().Push(ops)
paint.Fill(ops, strokecolor)
stack.Pop()
}
// AbsCircle makes a circle centered at (x, y), radius r
func (c *Canvas) AbsCircle(x, y, radius float32, fillcolor color.NRGBA) {
path := new(clip.Path)
ops := c.Context.Ops
const k = 0.551915024494 // http://spencermortensen.com/articles/bezier-circle/
path.Begin(ops)
path.Move(f32.Point{X: x + radius, Y: y})
path.Cube(f32.Point{X: 0, Y: radius * k}, f32.Point{X: -radius + radius*k, Y: radius}, f32.Point{X: -radius, Y: radius}) // SE
path.Cube(f32.Point{X: -radius * k, Y: 0}, f32.Point{X: -radius, Y: -radius + radius*k}, f32.Point{X: -radius, Y: -radius}) // SW
path.Cube(f32.Point{X: 0, Y: -radius * k}, f32.Point{X: radius - radius*k, Y: -radius}, f32.Point{X: radius, Y: -radius}) // NW
path.Cube(f32.Point{X: radius * k, Y: 0}, f32.Point{X: radius, Y: radius - radius*k}, f32.Point{X: radius, Y: radius}) // NE
path.Close()
stack := clip.Outline{Path: path.End()}.Op().Push(ops)
paint.ColorOp{Color: fillcolor}.Add(ops)
paint.PaintOp{}.Add(ops)
stack.Pop()
}
// AbsEllipse makes a ellipse centered at (x, y) radii (w, h)
func (c *Canvas) AbsEllipse(x, y, w, h float32, fillcolor color.NRGBA) {
path := new(clip.Path)
ops := c.Context.Ops
const k = 0.551915024494 // http://spencermortensen.com/articles/bezier-circle/
path.Begin(ops)
path.Move(f32.Point{X: x + w, Y: y})
path.Cube(f32.Point{X: 0, Y: h * k}, f32.Point{X: -w + w*k, Y: h}, f32.Point{X: -w, Y: h}) // SE
path.Cube(f32.Point{X: -w * k, Y: 0}, f32.Point{X: -w, Y: -h + h*k}, f32.Point{X: -w, Y: -h}) // SW
path.Cube(f32.Point{X: 0, Y: -h * k}, f32.Point{X: w - w*k, Y: -h}, f32.Point{X: w, Y: -h}) // NW
path.Cube(f32.Point{X: w * k, Y: 0}, f32.Point{X: w, Y: h - h*k}, f32.Point{X: w, Y: h}) // NE
stack := clip.Outline{Path: path.End()}.Op().Push(ops)
paint.ColorOp{Color: fillcolor}.Add(ops)
paint.PaintOp{}.Add(ops)
stack.Pop()
}
// AbsArc makes circular arc centered at (x, y), through angles start and end;
// the angles are measured in radians and increase counter-clockwise.
// N.B: derived from the clipLoader function in widget/material/loader.go
func (c *Canvas) AbsArc(x, y, radius float32, start, end float64, fillcolor color.NRGBA) {
ops := c.Context.Ops
sine, cose := math.Sincos(start)
path := new(clip.Path)
path.Begin(ops)
path.Move(f32.Pt(x, y)) // move to the center
pen := f32.Pt(float32(cose), float32(sine)).Mul(radius) // starting point
path.Line(pen)
// The clip path uses quadratic beziér curves to approximate
// a circle arc. Minimize the error by capping the length of
// each curve segment.
const maxArcLen = 20.0
arcPerRadian := float64(radius) * math.Pi
anglePerSegment := maxArcLen / arcPerRadian
for angle := start; angle < end; {
angle += anglePerSegment
if angle > end {
angle = end
}
sins, coss := sine, cose
sine, cose = math.Sincos(angle)
// https://pomax.github.io/bezierinfo/#circles
div := 1.0 / (coss*sine - cose*sins)
ctrlPt := f32.Point{X: float32((sine - sins) * div), Y: -float32((cose - coss) * div)}.Mul(radius)
endPt := f32.Pt(float32(cose), float32(sine)).Mul(radius)
path.Quad(ctrlPt.Sub(pen), endPt.Sub(pen))
pen = endPt
}
path.Close()
stack := clip.Outline{Path: path.End()}.Op().Push(ops)
paint.ColorOp{Color: fillcolor}.Add(ops)
paint.PaintOp{}.Add(ops)
stack.Pop()
}
// AbsTranslate moves current location by (x,y)
func (c *Canvas) AbsTranslate(x, y float32) op.TransformStack {
ops := c.Context.Ops
//op.InvalidateOp{}.Add(ops)
c.Context.Execute(op.InvalidateCmd{})
stack := op.Offset(image.Pt(0, 0)).Push(ops)
tr := f32.Affine2D{}
tr = tr.Offset(f32.Pt(x, y))
op.Affine(tr).Add(ops)
return stack
}
// AbsRotate rotates around (x,y) using angle (radians)
func (c *Canvas) AbsRotate(x, y, angle float32) op.TransformStack {
ops := c.Context.Ops
//op.InvalidateOp{}.Add(ops)
c.Context.Execute(op.InvalidateCmd{})
stack := op.Offset(image.Pt(0, 0)).Push(ops)
tr := f32.Affine2D{}.Rotate(f32.Pt(x, y), angle)
op.Affine(tr).Add(ops)
return stack
}
// AbsScale scales by factor at (x,y)
func (c *Canvas) AbsScale(x, y, factor float32) op.TransformStack {
ops := c.Context.Ops
//op.InvalidateOp{}.Add(ops)
c.Context.Execute(op.InvalidateCmd{})
stack := op.Offset(image.Pt(0, 0)).Push(ops)
tr := f32.Affine2D{}.Scale(f32.Pt(x, y), f32.Pt(factor, factor))
op.Affine(tr).Add(ops)
return stack
}
// AbsShear shears at (x,y) using angle ax and ay
func (c *Canvas) AbsShear(x, y, ax, ay float32) op.TransformStack {
ops := c.Context.Ops
//op.InvalidateOp{}.Add(ops)
c.Context.Execute(op.InvalidateCmd{})
stack := op.Offset(image.Pt(0, 0)).Push(ops)
tr := f32.Affine2D{}.Shear(f32.Pt(x, y), ax, ay)
op.Affine(tr).Add(ops)
return stack
}