/
canvas.go
202 lines (176 loc) · 4.75 KB
/
canvas.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
package giopdf
import (
"image"
"image/color"
"gioui.org/f32"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"github.com/andybalholm/stroke"
)
// A Canvas implements the PDF imaging model, drawing to a Gio operations list.
// Most of its methods correspond directly to PDF page description operators.
type Canvas struct {
PathBuilder
graphicsState
stateStack []graphicsState
setClippingPath bool
ops *op.Ops
}
func NewCanvas(ops *op.Ops) *Canvas {
return &Canvas{
ops: ops,
graphicsState: graphicsState{
fillColor: color.NRGBA{0, 0, 0, 255},
strokeColor: color.NRGBA{0, 0, 0, 255},
lineWidth: 1,
miterLimit: 10,
hScale: 100,
},
}
}
func (c *Canvas) fill() {
ps := toPathSpec(c.ops, c.Path, true)
paint.FillShape(c.ops, c.fillColor, clip.Outline{ps}.Op())
}
func (c *Canvas) stroke() {
var p [][]stroke.Segment
var contour []stroke.Segment
var pos, lastMove f32.Point
for _, e := range c.Path {
switch e.Op {
case 'm':
lastMove = e.End
pos = e.End
if len(contour) > 0 {
p = append(p, contour)
contour = nil
}
case 'l':
contour = append(contour, stroke.LinearSegment(stroke.Point(pos), stroke.Point(e.End)))
pos = e.End
case 'c':
contour = append(contour, stroke.Segment{stroke.Point(pos), stroke.Point(e.CP1), stroke.Point(e.CP2), stroke.Point(e.End)})
pos = e.End
case 'h':
if pos != lastMove {
contour = append(contour, stroke.LinearSegment(stroke.Point(pos), stroke.Point(lastMove)))
pos = lastMove
}
}
}
if len(contour) > 0 {
p = append(p, contour)
contour = nil
}
if len(c.dashes) > 0 {
p = stroke.Dash(p, c.dashes, c.dashPhase)
}
outline := stroke.Stroke(p, stroke.Options{
Width: c.lineWidth,
Cap: stroke.CapStyle(c.lineCap),
Join: stroke.JoinStyle(c.lineJoin),
MiterLimit: c.miterLimit,
})
ps := segmentsToPathSpec(c.ops, outline)
paint.FillShape(c.ops, c.strokeColor, clip.Outline{ps}.Op())
}
func segmentsToPathSpec(ops *op.Ops, outline [][]stroke.Segment) clip.PathSpec {
var path clip.Path
path.Begin(ops)
for _, contour := range outline {
path.MoveTo(f32.Point(contour[0].Start))
for i, s := range contour {
if i > 0 && s.Start != contour[i-1].End {
path.LineTo(f32.Point(s.Start))
}
path.CubeTo(f32.Point(s.CP1), f32.Point(s.CP2), f32.Point(s.End))
}
}
return path.End()
}
func (c *Canvas) finishPath() {
if c.setClippingPath {
ps := toPathSpec(c.ops, c.Path, true)
cs := clip.Outline{ps}.Op().Push(c.ops)
c.clippingPaths = append(c.clippingPaths, cs)
}
c.setClippingPath = false
c.Path = c.Path[:0]
}
// Fill fills the current path.
func (c *Canvas) Fill() {
c.fill()
c.finishPath()
}
// Stroke strokes (outlines) the current path.
func (c *Canvas) Stroke() {
c.stroke()
c.finishPath()
}
// CloseAndStroke closes the current path before stroking it it.
func (c *Canvas) CloseAndStroke() {
c.ClosePath()
c.stroke()
c.finishPath()
}
// FillAndStroke fills the current path and then strokes (outlines) it.
func (c *Canvas) FillAndStroke() {
c.fill()
c.stroke()
c.finishPath()
}
// NoOpPaint finishes the current path without filling or stroking it.
// It is normally used to apply a clipping path after calling Clip.
func (c *Canvas) NoOpPaint() {
c.finishPath()
}
// Clip causes the current path to be added to the clipping path after it is
// painted.
func (c *Canvas) Clip() {
c.setClippingPath = true
}
// CloseFillAndStroke closes the current path before filling and stroking it.
func (c *Canvas) CloseFillAndStroke() {
c.ClosePath()
c.fill()
c.stroke()
c.finishPath()
}
// Save pushes a copy of the current graphics state onto the state stack.
func (c *Canvas) Save() {
c.stateStack = append(c.stateStack, c.graphicsState)
c.transforms = nil
c.clippingPaths = nil
}
// Restore restores the graphics state, popping it off the stack.
func (c *Canvas) Restore() {
// First pop off the TransformStack and clip.Stack entries that were saved since the last Save call.
for i := len(c.transforms) - 1; i >= 0; i-- {
c.transforms[i].Pop()
}
for i := len(c.clippingPaths) - 1; i >= 0; i-- {
c.clippingPaths[i].Pop()
}
n := len(c.stateStack) - 1
c.graphicsState = c.stateStack[n]
c.stateStack = c.stateStack[:n]
}
// Transform changes the coordinate system according to the transformation
// matrix specified.
func (ca *Canvas) Transform(a, b, c, d, e, f float32) {
m := f32.NewAffine2D(a, c, e, b, d, f)
s := op.Affine(m).Push(ca.ops)
ca.transforms = append(ca.transforms, s)
}
// Image draws an image. The image is placed in the unit square of the user
// coordinate system.
func (c *Canvas) Image(img image.Image) {
io := paint.NewImageOp(img)
size := io.Size()
c.Save()
c.Transform(1/float32(size.X), 0, 0, -1/float32(size.Y), 0, 1)
io.Add(c.ops)
paint.PaintOp{}.Add(c.ops)
c.Restore()
}