forked from tdewolff/canvas
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcanvas.go
653 lines (560 loc) · 20.5 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
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
package canvas
import (
"image"
"image/color"
"io"
"os"
"sort"
)
//const mmPerPx = 25.4 / 96.0
//const pxPerMm = 96.0 / 25.4
const mmPerPt = 25.4 / 72.0
const ptPerMm = 72.0 / 25.4
const mmPerInch = 25.4
const inchPerMm = 1.0 / 25.4
const PtToMM = mmPerPt
// Resolution is used for rasterizing. Higher resolutions will result in larger images.
type Resolution float64
// DPMM (dots-per-millimeter) for the resolution of rasterization.
func DPMM(dpmm float64) Resolution {
return Resolution(dpmm)
}
// DPI (dots-per-inch) for the resolution of rasterization.
func DPI(dpi float64) Resolution {
return Resolution(dpi * inchPerMm)
}
// DPMM returns the resolution in dots-per-millimeter.
func (res Resolution) DPMM() float64 {
return float64(res)
}
// DPI returns the resolution in dots-per-inch.
func (res Resolution) DPI() float64 {
return float64(res) * mmPerInch
}
// DefaultResolution is the default resolution used for font PPEMs and is set to 96 DPI.
const DefaultResolution = Resolution(96.0 * inchPerMm)
// Size defines a size (width and height).
type Size struct {
W, H float64
}
var (
A0 = Size{841.0, 1189.0}
A1 = Size{594.0, 841.0}
A2 = Size{420.0, 594.0}
A3 = Size{297.0, 420.0}
A4 = Size{210.0, 297.0}
A5 = Size{148.0, 210.0}
A6 = Size{105.0, 148.0}
A7 = Size{74.0, 105.0}
A8 = Size{52.0, 74.0}
B0 = Size{1000.0, 1414.0}
B1 = Size{707.0, 1000.0}
B2 = Size{500.0, 707.0}
B3 = Size{353.0, 500.0}
B4 = Size{250.0, 353.0}
B5 = Size{176.0, 250.0}
B6 = Size{125.0, 176.0}
B7 = Size{88.0, 125.0}
B8 = Size{62.0, 88.0}
B9 = Size{44.0, 62.0}
B10 = Size{31.0, 44.0}
C2 = Size{648.0, 458.0}
C3 = Size{458.0, 324.0}
C4 = Size{324.0, 229.0}
C5 = Size{229.0, 162.0}
C6 = Size{162.0, 114.0}
D0 = Size{1090.0, 771.0}
SRA0 = Size{1280.0, 900.0}
SRA1 = Size{900.0, 640.0}
SRA2 = Size{640.0, 450.0}
SRA3 = Size{450.0, 320.0}
SRA4 = Size{320.0, 225.0}
RA0 = Size{1220.0, 860.0}
RA1 = Size{860.0, 610.0}
RA2 = Size{610.0, 430.0}
Letter = Size{215.9, 279.4}
Legal = Size{215.9, 355.6}
Ledger = Size{279.4, 431.8}
Tabloid = Size{431.8, 279.4}
Executive = Size{184.1, 266.7}
)
////////////////////////////////////////////////////////////////
// Style is the path style that defines how to draw the path. When FillColor is transparent it will not fill the path. If StrokeColor is transparent or StrokeWidth is zero, it will not stroke the path. If Dashes is an empty array, it will not draw dashes but instead a solid stroke line. FillRule determines how to fill the path when paths overlap and have certain directions (clockwise, counter clockwise).
type Style struct {
FillColor color.RGBA
StrokeColor color.RGBA
StrokeWidth float64
StrokeCapper Capper
StrokeJoiner Joiner
DashOffset float64
Dashes []float64
FillRule // TODO: test for all renderers
}
// HasFill returns true if the style has a fill
func (style Style) HasFill() bool {
return style.FillColor.A != 0
}
// HasStroke returns true if the style has a stroke
func (style Style) HasStroke() bool {
return style.StrokeColor.A != 0 && 0.0 < style.StrokeWidth
}
// IsDashed returns true if the style has dashes
func (style Style) IsDashed() bool {
return 0 < len(style.Dashes)
}
// DefaultStyle is the default style for paths. It fills the path with a black color and has no stroke.
var DefaultStyle = Style{
FillColor: Black,
StrokeColor: Transparent,
StrokeWidth: 1.0,
StrokeCapper: ButtCap,
StrokeJoiner: MiterJoin,
DashOffset: 0.0,
Dashes: []float64{},
FillRule: NonZero,
}
// Renderer is an interface that renderers implement. It defines the size of the target (in mm) and functions to render paths, text objects and images.
type Renderer interface {
Size() (float64, float64)
RenderPath(path *Path, style Style, m Matrix)
RenderText(text *Text, m Matrix)
RenderImage(img image.Image, m Matrix)
}
////////////////////////////////////////////////////////////////
// CoordSystem is the coordinate system, which can be either of the four cartesian quadrants. Most useful are the I'th and IV'th quadrants. CartesianI is the default quadrant with the zero-point in the bottom-left (the default for mathematics). The CartesianII has its zero-point in the bottom-right, CartesianIII in the top-right, and CartesianIV in the top-left (often used as default for printing devices). See https://en.wikipedia.org/wiki/Cartesian_coordinate_system#Quadrants_and_octants for an explanation.
type CoordSystem int
// see CoordSystem
const (
CartesianI CoordSystem = iota
CartesianIV
)
type ContextState struct {
Style
view Matrix
coordView Matrix
coordSystem CoordSystem
}
// Context maintains the state for the current path, path style, and view transformation matrix.
type Context struct {
Renderer
path *Path
ContextState
stack []ContextState
}
// NewContext returns a new context which is a wrapper around a renderer. Contexts maintain the state of the current path, path style, and view transformation matrix.
func NewContext(r Renderer) *Context {
return &Context{
Renderer: r,
path: &Path{},
ContextState: ContextState{
Style: DefaultStyle,
view: Identity,
coordView: Identity,
coordSystem: CartesianIV,
},
stack: nil,
}
}
// Width returns the width of the canvas in millimeters.
func (c *Context) Width() float64 {
w, _ := c.Renderer.Size()
return w
}
// Height returns the height of the canvas in millimeters.
func (c *Context) Height() float64 {
_, h := c.Renderer.Size()
return h
}
// Size returns the width and height of the canvas in millimeters.
func (c *Context) Size() (float64, float64) {
return c.Renderer.Size()
}
// Push saves the current draw state so that it can be popped later on.
func (c *Context) Push() {
c.stack = append(c.stack, c.ContextState)
}
// Pop restores the last pushed draw state and uses that as the current draw state. If there are no states on the stack, this will do nothing.
func (c *Context) Pop() {
if len(c.stack) == 0 {
return
}
c.ContextState = c.stack[len(c.stack)-1]
c.stack = c.stack[:len(c.stack)-1]
}
// CoordView returns the current affine transformation matrix through which all operation coordinates will be transformed.
func (c *Context) CoordView() Matrix {
return c.coordView
}
// SetCoordView sets the current affine transformation matrix through which all operation coordinates will be transformed. See `Matrix` for how transformations work.
func (c *Context) SetCoordView(coordView Matrix) {
c.coordView = coordView
}
// SetCoordRect sets the current affine transformation matrix through which all operation coordinates will be transformed. It will transform coordinates from (0,0)--(width,height) to the target `rect`.
func (c *Context) SetCoordRect(rect Rect, width, height float64) {
c.coordView = Identity.Translate(rect.X, rect.Y).Scale(rect.W/width, rect.H/height)
}
// SetCoordSystem sets the current affine transformation matrix through which all operation coordinates will be transformed as a Cartesian coordinate system.
func (c *Context) SetCoordSystem(coordSystem CoordSystem) {
c.coordSystem = coordSystem
}
// View returns the current affine transformation matrix through which all operations will be transformed.
func (c *Context) View() Matrix {
return c.view
}
// SetView sets the current affine transformation matrix through which all operations will be transformed. See `Matrix` for how transformations work.
func (c *Context) SetView(view Matrix) {
c.view = view
}
// ResetView resets the current affine transformation matrix to the Identity matrix, ie. no transformations.
func (c *Context) ResetView() {
c.view = Identity
}
// ComposeView post-multiplies the current affine transformation matrix by the given matrix. This means that any draw action will first be transformed by the new view matrix (parameter) and then by the current view matrix (ie. `Context.View()`). `Context.ComposeView(Identity.ReflectX())` is the same as `Context.ReflectX()`.
func (c *Context) ComposeView(view Matrix) {
c.view = c.view.Mul(view)
}
// Translate moves the view.
func (c *Context) Translate(x, y float64) {
c.view = c.view.Mul(Identity.Translate(x, y))
}
// ReflectX inverts the X axis of the view.
func (c *Context) ReflectX() {
c.view = c.view.Mul(Identity.ReflectX())
}
// ReflectXAbout inverts the X axis of the view about the given X coordinate.
func (c *Context) ReflectXAbout(x float64) {
c.view = c.view.Mul(Identity.ReflectXAbout(x))
}
// ReflectY inverts the Y axis of the view.
func (c *Context) ReflectY() {
c.view = c.view.Mul(Identity.ReflectY())
}
// ReflectYAbout inverts the Y axis of the view about the given Y coordinate.
func (c *Context) ReflectYAbout(y float64) {
c.view = c.view.Mul(Identity.ReflectYAbout(y))
}
// Rotate rotates the view counter clockwise with rot in degrees.
func (c *Context) Rotate(rot float64) {
c.view = c.view.Mul(Identity.Rotate(rot))
}
// RotateAbout rotates the view counter clockwise around (x,y) with rot in degrees.
func (c *Context) RotateAbout(rot, x, y float64) {
c.view = c.view.Mul(Identity.RotateAbout(rot, x, y))
}
// Scale scales the view.
func (c *Context) Scale(sx, sy float64) {
c.view = c.view.Mul(Identity.Scale(sx, sy))
}
// ScaleAbout scales the view around (x,y).
func (c *Context) ScaleAbout(sx, sy, x, y float64) {
c.view = c.view.Mul(Identity.ScaleAbout(sx, sy, x, y))
}
// Shear shear stretches the view.
func (c *Context) Shear(sx, sy float64) {
c.view = c.view.Mul(Identity.Shear(sx, sy))
}
// ShearAbout shear stretches the view around (x,y).
func (c *Context) ShearAbout(sx, sy, x, y float64) {
c.view = c.view.Mul(Identity.ShearAbout(sx, sy, x, y))
}
// SetFillColor sets the color to be used for filling operations.
func (c *Context) SetFillColor(col color.Color) {
r, g, b, a := col.RGBA()
// RGBA returns an alpha-premultiplied color so that c <= a. We silently correct the color by clipping r,g,b to a
if a < r {
r = a
}
if a < g {
g = a
}
if a < b {
b = a
}
c.Style.FillColor = color.RGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)}
}
// SetStrokeColor sets the color to be used for stroking operations.
func (c *Context) SetStrokeColor(col color.Color) {
r, g, b, a := col.RGBA()
// RGBA returns an alpha-premultiplied color so that c <= a. We silently correct the color by clipping r,g,b to a
if a < r {
r = a
}
if a < g {
g = a
}
if a < b {
b = a
}
c.Style.StrokeColor = color.RGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)}
}
// SetStrokeWidth sets the width in millimeters for stroking operations.
func (c *Context) SetStrokeWidth(width float64) {
c.Style.StrokeWidth = width
}
// SetStrokeCapper sets the line cap function to be used for stroke end points.
func (c *Context) SetStrokeCapper(capper Capper) {
c.Style.StrokeCapper = capper
}
// SetStrokeJoiner sets the line join function to be used for stroke mid points.
func (c *Context) SetStrokeJoiner(joiner Joiner) {
c.Style.StrokeJoiner = joiner
}
// SetDashes sets the dash pattern to be used for stroking operations. The dash offset denotes the offset into the dash array in millimeters from where to start. Negative values are allowed.
func (c *Context) SetDashes(offset float64, dashes ...float64) {
c.Style.DashOffset = offset
c.Style.Dashes = dashes
}
// SetFillRule sets the fill rule to be used for filling paths.
func (c *Context) SetFillRule(rule FillRule) {
c.Style.FillRule = rule
}
// ResetStyle resets the draw state to its default (colors, stroke widths, dashes, ...).
func (c *Context) ResetStyle() {
c.Style = DefaultStyle
}
// SetZIndex sets the z-index. This will call the renderer's `SetZIndex` function only if it exists (in this case only for `Canvas`).
func (c *Context) SetZIndex(zindex int) {
if zindexer, ok := c.Renderer.(interface{ SetZIndex(int) }); ok {
zindexer.SetZIndex(zindex)
}
}
// Pos returns the current position of the path, which is the end point of the last command.
func (c *Context) Pos() (float64, float64) {
return c.path.Pos().X, c.path.Pos().Y
}
// MoveTo moves the path to (x,y) without connecting with the previous path. It starts a new independent subpath. Multiple subpaths can be useful when negating parts of a previous path by overlapping it with a path in the opposite direction. The behaviour of overlapping paths depends on the FillRule.
func (c *Context) MoveTo(x, y float64) {
c.path.MoveTo(x, y)
}
// LineTo adds a linear path to (x,y).
func (c *Context) LineTo(x, y float64) {
c.path.LineTo(x, y)
}
// QuadTo adds a quadratic Bézier path with control point (cpx,cpy) and end point (x,y).
func (c *Context) QuadTo(cpx, cpy, x, y float64) {
c.path.QuadTo(cpx, cpy, x, y)
}
// CubeTo adds a cubic Bézier path with control points (cpx1,cpy1) and (cpx2,cpy2) and end point (x,y).
func (c *Context) CubeTo(cpx1, cpy1, cpx2, cpy2, x, y float64) {
c.path.CubeTo(cpx1, cpy1, cpx2, cpy2, x, y)
}
// ArcTo adds an arc with radii rx and ry, with rot the counter clockwise rotation with respect to the coordinate system in degrees, large and sweep booleans (see https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths#Arcs), and (x,y) the end position of the pen. The start position of the pen was given by a previous command's end point.
func (c *Context) ArcTo(rx, ry, rot float64, large, sweep bool, x, y float64) {
c.path.ArcTo(rx, ry, rot, large, sweep, x, y)
}
// Arc adds an elliptical arc with radii rx and ry, with rot the counter clockwise rotation in degrees, and theta0 and theta1 the angles in degrees of the ellipse (before rot is applied) between which the arc will run. If theta0 < theta1, the arc will run in a CCW direction. If the difference between theta0 and theta1 is bigger than 360 degrees, one full circle will be drawn and the remaining part of diff % 360, e.g. a difference of 810 degrees will draw one full circle and an arc over 90 degrees.
func (c *Context) Arc(rx, ry, rot, theta0, theta1 float64) {
c.path.Arc(rx, ry, rot, theta0, theta1)
}
// Close closes the current path.
func (c *Context) Close() {
c.path.Close()
}
// Fill fills the current path and resets the path.
func (c *Context) Fill() {
strokeColor := c.Style.StrokeColor
c.Style.StrokeColor = Transparent
c.DrawPath(0.0, 0.0, c.path)
c.Style.StrokeColor = strokeColor
c.path = &Path{}
}
// Stroke strokes the current path and resets the path.
func (c *Context) Stroke() {
fillColor := c.Style.FillColor
c.Style.FillColor = Transparent
c.DrawPath(0.0, 0.0, c.path)
c.Style.FillColor = fillColor
c.path = &Path{}
}
// FillStroke fills and then strokes the current path and resets the path.
func (c *Context) FillStroke() {
c.DrawPath(0.0, 0.0, c.path)
c.path = &Path{}
}
// DrawPath draws a path at position (x,y) using the current draw state.
func (c *Context) DrawPath(x, y float64, paths ...*Path) {
if !c.Style.HasFill() && !c.Style.HasStroke() {
return
}
coord := c.coordView.Dot(Point{x, y})
m := Identity
if c.coordSystem == CartesianIV {
m = m.ReflectYAbout(c.Height() / 2.0)
}
m = m.Mul(c.view).Translate(coord.X, coord.Y)
for _, path := range paths {
var dashes []float64
path, dashes = path.checkDash(c.Style.DashOffset, c.Style.Dashes)
if path.Empty() {
continue
}
style := c.Style
style.Dashes = dashes
c.RenderPath(path, style, m)
}
}
// DrawText draws text at position (x,y) using the current draw state.
func (c *Context) DrawText(x, y float64, texts ...*Text) {
coordView := Identity
if c.coordSystem == CartesianIV {
coordView = coordView.ReflectYAbout(c.Height() / 2.0)
}
coord := coordView.Mul(c.coordView).Dot(Point{x, y})
m := c.view.Translate(coord.X, coord.Y)
for _, text := range texts {
if text.Empty() {
continue
}
c.RenderText(text, m)
}
}
// DrawImage draws an image at position (x,y) using the current draw state and the given resolution in pixels-per-millimeter. A higher resolution will draw a smaller image (ie. more image pixels per millimeter of document).
func (c *Context) DrawImage(x, y float64, img image.Image, resolution Resolution) {
if img.Bounds().Size().Eq(image.Point{}) {
return
}
var coord Point
if c.coordSystem == CartesianI {
coord = c.coordView.Dot(Point{x, y})
} else if c.coordSystem == CartesianIV {
coord = Identity.ReflectYAbout(c.Height() / 2.0).Mul(c.coordView).Dot(Point{x, y})
}
m := c.view.Translate(coord.X, coord.Y).Scale(1.0/resolution.DPMM(), 1.0/resolution.DPMM())
if c.coordSystem == CartesianIV {
m = m.Translate(0.0, -float64(img.Bounds().Size().Y))
}
c.RenderImage(img, m)
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
type layer struct {
// path, text OR img is set
path *Path
text *Text
img image.Image
m Matrix
style Style // only for path
}
// Canvas stores all drawing operations as layers that can be re-rendered to other renderers.
type Canvas struct {
layers map[int][]layer
zindex int
W, H float64
}
// New returns a new canvas with width and height in millimeters, that records all drawing operations into layers. The canvas can then be rendered to any other renderer.
func New(width, height float64) *Canvas {
return &Canvas{
layers: map[int][]layer{},
W: width,
H: height,
}
}
// NewFromSize returns a new canvas of given size in millimeters, that records all drawing operations into layers. The canvas can then be rendered to any other renderer.
func NewFromSize(size Size) *Canvas {
return New(size.W, size.H)
}
// Size returns the size of the canvas in millimeters.
func (c *Canvas) Size() (float64, float64) {
return c.W, c.H
}
// RenderPath renders a path to the canvas using a style and a transformation matrix.
func (c *Canvas) RenderPath(path *Path, style Style, m Matrix) {
path = path.Copy()
c.layers[c.zindex] = append(c.layers[c.zindex], layer{path: path, m: m, style: style})
}
// RenderText renders a text object to the canvas using a transformation matrix.
func (c *Canvas) RenderText(text *Text, m Matrix) {
c.layers[c.zindex] = append(c.layers[c.zindex], layer{text: text, m: m})
}
// RenderImage renders an image to the canvas using a transformation matrix.
func (c *Canvas) RenderImage(img image.Image, m Matrix) {
c.layers[c.zindex] = append(c.layers[c.zindex], layer{img: img, m: m})
}
// Empty return true if the canvas is empty.
func (c *Canvas) Empty() bool {
return len(c.layers) == 0
}
// Reset empties the canvas.
func (c *Canvas) Reset() {
c.layers = map[int][]layer{}
}
// SetZIndex sets the z-index.
func (c *Canvas) SetZIndex(zindex int) {
c.zindex = zindex
}
// Fit shrinks the canvas' size so all elements fit with a given margin in millimeters.
func (c *Canvas) Fit(margin float64) {
rect := Rect{}
// TODO: slow when we have many paths (see Graph example)
for _, layers := range c.layers {
for i, l := range layers {
bounds := Rect{}
if l.path != nil {
bounds = l.path.Bounds()
if l.style.StrokeColor.A != 0 && 0.0 < l.style.StrokeWidth {
bounds.X -= l.style.StrokeWidth / 2.0
bounds.Y -= l.style.StrokeWidth / 2.0
bounds.W += l.style.StrokeWidth
bounds.H += l.style.StrokeWidth
}
} else if l.text != nil {
bounds = l.text.Bounds()
} else if l.img != nil {
size := l.img.Bounds().Size()
bounds = Rect{0.0, 0.0, float64(size.X), float64(size.Y)}
}
bounds = bounds.Transform(l.m)
if i == 0 {
rect = bounds
} else {
rect = rect.Add(bounds)
}
}
}
for _, layers := range c.layers {
for i := range layers {
layers[i].m = Identity.Translate(-rect.X+margin, -rect.Y+margin).Mul(layers[i].m)
}
}
c.W = rect.W + 2*margin
c.H = rect.H + 2*margin
}
// Render renders the accumulated canvas drawing operations to another renderer.
func (c *Canvas) Render(r Renderer) {
view := Identity
if viewer, ok := r.(interface{ View() Matrix }); ok {
view = viewer.View()
}
zindices := []int{}
for zindex := range c.layers {
zindices = append(zindices, zindex)
}
sort.Ints(zindices)
for _, zindex := range zindices {
for _, l := range c.layers[zindex] {
m := view.Mul(l.m)
if l.path != nil {
r.RenderPath(l.path, l.style, m)
} else if l.text != nil {
r.RenderText(l.text, m)
} else if l.img != nil {
r.RenderImage(l.img, m)
}
}
}
}
// Writer can write a canvas to a writer.
type Writer func(w io.Writer, c *Canvas) error
// WriteFile writes the canvas to a file named by filename using the given writer.
func (c *Canvas) WriteFile(filename string, w Writer) error {
f, err := os.Create(filename)
if err != nil {
return err
}
if err = w(f, c); err != nil {
f.Close()
return err
}
return f.Close()
}