forked from gonum/plot
/
recorder.go
489 lines (394 loc) · 11.1 KB
/
recorder.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
// Copyright ©2015 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package recorder provides support for vector graphics serialization.
package recorder // import "github.com/emptywe/plot/vg/recorder"
import (
"bytes"
"encoding/base64"
"fmt"
"image"
"image/color"
"image/png"
"runtime"
"github.com/emptywe/plot/font"
"github.com/emptywe/plot/font/liberation"
"github.com/emptywe/plot/vg"
)
var _ vg.Canvas = (*Canvas)(nil)
// Canvas implements vg.Canvas operation serialization.
type Canvas struct {
// Actions holds a log of all methods called on
// the canvas.
Actions []Action
// KeepCaller indicates whether the Canvas will
// retain runtime caller location for the actions.
// This includes source filename and line number.
KeepCaller bool
// c holds a backing vg.Canvas. If c is non-nil
// then method calls to the Canvas will be
// reflected to c.
c vg.Canvas
// fonts holds a collection of font/size descriptions.
fonts map[fontID]font.Face
cache *font.Cache
}
type fontID struct {
name string
size vg.Length
}
// Action is a vector graphics action as defined by the
// vg.Canvas interface. Each method of Canvas has a
// corresponding Action type.
type Action interface {
Call() string
ApplyTo(vg.Canvas)
callerLocation() *callerLocation
}
type callerLocation struct {
haveCaller bool
file string
line int
}
func (l *callerLocation) set() {
_, l.file, l.line, l.haveCaller = runtime.Caller(3)
}
func (l callerLocation) String() string {
if !l.haveCaller {
return ""
}
return fmt.Sprintf("%s:%d ", l.file, l.line)
}
// Reset resets the Canvas to the base state.
func (c *Canvas) Reset() {
c.Actions = c.Actions[:0]
}
// ReplayOn applies the set of Actions recorded by the Recorder onto
// the destination Canvas.
func (c *Canvas) ReplayOn(dst vg.Canvas) error {
if c.fonts == nil {
c.fonts = make(map[fontID]font.Face)
}
if c.cache == nil {
c.cache = font.NewCache(liberation.Collection())
}
for _, a := range c.Actions {
fa, ok := a.(*FillString)
if !ok {
continue
}
f := fontID{name: fa.Font.Name(), size: fa.Size}
if _, exists := c.fonts[f]; !exists {
if !c.cache.Has(fa.Font) {
return fmt.Errorf("unknown font: %s", fa.Font.Typeface)
}
face := c.cache.Lookup(
fa.Font,
fa.Size,
)
c.fonts[f] = face
}
fa.fonts = c.fonts
}
for _, a := range c.Actions {
a.ApplyTo(dst)
}
return nil
}
func (c *Canvas) append(a Action) {
if c.c != nil {
a.ApplyTo(c)
}
if c.KeepCaller {
a.callerLocation().set()
}
c.Actions = append(c.Actions, a)
}
// SetLineWidth corresponds to the vg.Canvas.SetWidth method.
type SetLineWidth struct {
Width vg.Length
l callerLocation
}
// SetLineWidth implements the SetLineWidth method of the vg.Canvas interface.
func (c *Canvas) SetLineWidth(w vg.Length) {
c.append(&SetLineWidth{Width: w})
}
// Call returns the method call that generated the action.
func (a *SetLineWidth) Call() string {
return fmt.Sprintf("%sSetLineWidth(%v)", a.l, a.Width)
}
// ApplyTo applies the action to the given vg.Canvas.
func (a *SetLineWidth) ApplyTo(c vg.Canvas) {
c.SetLineWidth(a.Width)
}
func (a *SetLineWidth) callerLocation() *callerLocation {
return &a.l
}
// SetLineDash corresponds to the vg.Canvas.SetLineDash method.
type SetLineDash struct {
Dashes []vg.Length
Offsets vg.Length
l callerLocation
}
// SetLineDash implements the SetLineDash method of the vg.Canvas interface.
func (c *Canvas) SetLineDash(dashes []vg.Length, offs vg.Length) {
c.append(&SetLineDash{
Dashes: append([]vg.Length(nil), dashes...),
Offsets: offs,
})
}
// Call returns the method call that generated the action.
func (a *SetLineDash) Call() string {
return fmt.Sprintf("%sSetLineDash(%#v, %v)", a.l, a.Dashes, a.Offsets)
}
// ApplyTo applies the action to the given vg.Canvas.
func (a *SetLineDash) ApplyTo(c vg.Canvas) {
c.SetLineDash(a.Dashes, a.Offsets)
}
func (a *SetLineDash) callerLocation() *callerLocation {
return &a.l
}
// SetColor corresponds to the vg.Canvas.SetColor method.
type SetColor struct {
Color color.Color
l callerLocation
}
// SetColor implements the SetColor method of the vg.Canvas interface.
func (c *Canvas) SetColor(col color.Color) {
c.append(&SetColor{Color: col})
}
// Call returns the method call that generated the action.
func (a *SetColor) Call() string {
return fmt.Sprintf("%sSetColor(%#v)", a.l, a.Color)
}
// ApplyTo applies the action to the given vg.Canvas.
func (a *SetColor) ApplyTo(c vg.Canvas) {
c.SetColor(a.Color)
}
func (a *SetColor) callerLocation() *callerLocation {
return &a.l
}
// Rotate corresponds to the vg.Canvas.Rotate method.
type Rotate struct {
Angle float64
l callerLocation
}
// Rotate implements the Rotate method of the vg.Canvas interface.
func (c *Canvas) Rotate(a float64) {
c.append(&Rotate{Angle: a})
}
// Call returns the method call that generated the action.
func (a *Rotate) Call() string {
return fmt.Sprintf("%sRotate(%v)", a.l, a.Angle)
}
// ApplyTo applies the action to the given vg.Canvas.
func (a *Rotate) ApplyTo(c vg.Canvas) {
c.Rotate(a.Angle)
}
func (a *Rotate) callerLocation() *callerLocation {
return &a.l
}
// Translate corresponds to the vg.Canvas.Translate method.
type Translate struct {
Point vg.Point
l callerLocation
}
// Translate implements the Translate method of the vg.Canvas interface.
func (c *Canvas) Translate(pt vg.Point) {
c.append(&Translate{Point: pt})
}
// Call returns the method call that generated the action.
func (a *Translate) Call() string {
return fmt.Sprintf("%sTranslate(%v, %v)", a.l, a.Point.X, a.Point.Y)
}
// ApplyTo applies the action to the given vg.Canvas.
func (a *Translate) ApplyTo(c vg.Canvas) {
c.Translate(a.Point)
}
func (a *Translate) callerLocation() *callerLocation {
return &a.l
}
// Scale corresponds to the vg.Canvas.Scale method.
type Scale struct {
X, Y float64
l callerLocation
}
// Scale implements the Scale method of the vg.Canvas interface.
func (c *Canvas) Scale(x, y float64) {
c.append(&Scale{X: x, Y: y})
}
// Call returns the method call that generated the action.
func (a *Scale) Call() string {
return fmt.Sprintf("%sScale(%v, %v)", a.l, a.X, a.Y)
}
// ApplyTo applies the action to the given vg.Canvas.
func (a *Scale) ApplyTo(c vg.Canvas) {
c.Scale(a.X, a.Y)
}
func (a *Scale) callerLocation() *callerLocation {
return &a.l
}
// Push corresponds to the vg.Canvas.Push method.
type Push struct {
l callerLocation
}
// Push implements the Push method of the vg.Canvas interface.
func (c *Canvas) Push() {
c.append(&Push{})
}
// Call returns the method call that generated the action.
func (a *Push) Call() string {
return fmt.Sprintf("%sPush()", a.l)
}
// ApplyTo applies the action to the given vg.Canvas.
func (a *Push) ApplyTo(c vg.Canvas) {
c.Push()
}
func (a *Push) callerLocation() *callerLocation {
return &a.l
}
// Pop corresponds to the vg.Canvas.Pop method.
type Pop struct {
l callerLocation
}
// Pop implements the Pop method of the vg.Canvas interface.
func (c *Canvas) Pop() {
c.append(&Pop{})
}
// Call returns the method call that generated the action.
func (a *Pop) Call() string {
return fmt.Sprintf("%sPop()", a.l)
}
// ApplyTo applies the action to the given vg.Canvas.
func (a *Pop) ApplyTo(c vg.Canvas) {
c.Pop()
}
func (a *Pop) callerLocation() *callerLocation {
return &a.l
}
// Stroke corresponds to the vg.Canvas.Stroke method.
type Stroke struct {
Path vg.Path
l callerLocation
}
// Stroke implements the Stroke method of the vg.Canvas interface.
func (c *Canvas) Stroke(path vg.Path) {
c.append(&Stroke{Path: append(vg.Path(nil), path...)})
}
// Call returns the method call that generated the action.
func (a *Stroke) Call() string {
return fmt.Sprintf("%sStroke(%#v)", a.l, a.Path)
}
// ApplyTo applies the action to the given vg.Canvas.
func (a *Stroke) ApplyTo(c vg.Canvas) {
c.Stroke(a.Path)
}
func (a *Stroke) callerLocation() *callerLocation {
return &a.l
}
// Fill corresponds to the vg.Canvas.Fill method.
type Fill struct {
Path vg.Path
l callerLocation
}
// Fill implements the Fill method of the vg.Canvas interface.
func (c *Canvas) Fill(path vg.Path) {
c.append(&Fill{Path: append(vg.Path(nil), path...)})
}
// Call returns the method call that generated the action.
func (a *Fill) Call() string {
return fmt.Sprintf("%sFill(%#v)", a.l, a.Path)
}
// ApplyTo applies the action to the given vg.Canvas.
func (a *Fill) ApplyTo(c vg.Canvas) {
c.Fill(a.Path)
}
func (a *Fill) callerLocation() *callerLocation {
return &a.l
}
// FillString corresponds to the vg.Canvas.FillString method.
type FillString struct {
Font font.Font
Size vg.Length
Point vg.Point
String string
l callerLocation
fonts map[fontID]font.Face
}
// FillString implements the FillString method of the vg.Canvas interface.
func (c *Canvas) FillString(font font.Face, pt vg.Point, str string) {
c.append(&FillString{
Font: font.Font,
Size: font.Font.Size,
Point: pt,
String: str,
})
}
// ApplyTo applies the action to the given vg.Canvas.
func (a *FillString) ApplyTo(c vg.Canvas) {
c.FillString(a.fonts[fontID{name: a.Font.Name(), size: a.Size}], a.Point, a.String)
}
// Call returns the pseudo method call that generated the action.
func (a *FillString) Call() string {
return fmt.Sprintf("%sFillString(%q, %v, %v, %v, %q)", a.l, a.Font.Name(), a.Size, a.Point.X, a.Point.Y, a.String)
}
func (a *FillString) callerLocation() *callerLocation {
return &a.l
}
// DrawImage corresponds to the vg.Canvas.DrawImage method
type DrawImage struct {
Rectangle vg.Rectangle
Image image.Image
l callerLocation
}
// DrawImage implements the DrawImage method of the vg.Canvas interface.
func (c *Canvas) DrawImage(rect vg.Rectangle, img image.Image) {
c.append(&DrawImage{
Rectangle: rect,
Image: img,
})
}
// ApplyTo applies the action to the given vg.Canvas.
func (a *DrawImage) ApplyTo(c vg.Canvas) {
c.DrawImage(a.Rectangle, a.Image)
}
// Call returns the pseudo method call that generated the action.
func (a *DrawImage) Call() string {
var buf bytes.Buffer
err := png.Encode(&buf, a.Image)
if err != nil {
panic(fmt.Errorf("recorder: error encoding image to PNG: %v", err))
}
b64 := base64.StdEncoding.EncodeToString(buf.Bytes())
return fmt.Sprintf("%sDrawImage(%#v, {%#v, IMAGE:%s})", a.l, a.Rectangle, a.Image.Bounds(), b64)
}
func (a *DrawImage) callerLocation() *callerLocation {
return &a.l
}
// Commenter defines types that can record comments.
type Commenter interface {
Comment(string)
}
var _ Commenter = (*Canvas)(nil)
// Comment implements a Recorder comment mechanism.
type Comment struct {
Text string
l callerLocation
}
// Comment adds a comment to a list of Actions..
func (c *Canvas) Comment(text string) {
c.append(&Comment{Text: text})
}
// Call returns the method call that generated the action.
func (a *Comment) Call() string {
return fmt.Sprintf("%sComment(%q)", a.l, a.Text)
}
// ApplyTo applies the action to the given vg.Canvas.
func (a *Comment) ApplyTo(c vg.Canvas) {
if c, ok := c.(Commenter); ok {
c.Comment(a.Text)
}
}
func (a *Comment) callerLocation() *callerLocation {
return &a.l
}