...
1
2
3
4
5 package vggio
6
7 import (
8 "image/color"
9
10 "gioui.org/f32"
11 "gioui.org/op"
12
13 "gonum.org/v1/plot/vg"
14 )
15
16
17 type ctxops struct {
18 ops *op.Ops
19 ctx []context
20
21 w vg.Length
22 h vg.Length
23 dpi float64
24 }
25
26
27
28
29 type context struct {
30 color color.Color
31 linew vg.Length
32 pattern []vg.Length
33 offset vg.Length
34
35 trans op.TransformStack
36 }
37
38 func (ctx *ctxops) cur() *context {
39 return &ctx.ctx[len(ctx.ctx)-1]
40 }
41
42 func (ctx *ctxops) push() {
43 ctx.ctx = append(ctx.ctx, *ctx.cur())
44 ctx.cur().trans = op.TransformOp{}.Push(ctx.ops)
45 }
46
47 func (ctx *ctxops) pop() {
48 ctx.cur().trans.Pop()
49 ctx.ctx = ctx.ctx[:len(ctx.ctx)-1]
50 }
51
52 func (ctx *ctxops) scale(x, y float64) {
53 op.Affine(f32.Affine2D{}.Scale(
54 f32.Pt(0, 0),
55 f32.Pt(float32(x), float32(y)),
56 )).Add(ctx.ops)
57 }
58
59 func (ctx *ctxops) translate(x, y float64) {
60 op.Affine(f32.Affine2D{}.Offset(
61 f32.Pt(float32(x), float32(y)),
62 )).Add(ctx.ops)
63 }
64
65 func (ctx *ctxops) rotate(rad float64) {
66 op.Affine(f32.Affine2D{}.Rotate(
67 f32.Pt(0, 0), float32(rad),
68 )).Add(ctx.ops)
69 }
70
71 func (ctx *ctxops) invertY() {
72 ctx.translate(0, ctx.h.Dots(ctx.dpi))
73 ctx.scale(1, -1)
74 }
75
76 func (ctx *ctxops) pt32(p vg.Point) f32.Point {
77 return f32.Point{
78 X: float32(p.X.Dots(ctx.dpi)),
79 Y: float32(p.Y.Dots(ctx.dpi)),
80 }
81 }
82
View as plain text