-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
canvas.go
509 lines (431 loc) · 15.7 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
package canvas
import (
"image"
"image/color"
"io"
"os"
)
const mmPerPt = 25.4 / 72
const ptPerMm = 72 / 25.4
const mmPerInch = 25.4
const inchPerMm = 1 / 25.4
// ImageEncoding defines whether the embedded image shall be embedded as Lossless (typically PNG) or Lossy (typically JPG).
type ImageEncoding int
// see ImageEncoding
const (
Lossless ImageEncoding = iota
Lossy
)
// DPMM (Dots-per-Millimetter) for the resolution of raster images. Higher DPMM will result in bigger images.
type DPMM float64
// DPI is a shortcut for Dots-per-Inch for the resolution of raster images.
const DPI = DPMM(1 / 25.4)
////////////////////////////////////////////////////////////////
// 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
}
// DefaultStyle is the default style for paths. It fills the path with a black color.
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 raster 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)
}
////////////////////////////////////////////////////////////////
type CoordSystem int
const (
CartesianI CoordSystem = iota
CartesianII
CartesianIII
CartesianIV
)
// Context maintains the state for the current path, path style, and view transformation matrix.
type Context struct {
Renderer
path *Path
Style
styleStack []Style
view Matrix
viewStack []Matrix
coordView Matrix
coordViewStack []Matrix
}
// NewContext returns a new Context which is a wrapper around a Renderer. Context maintains state for the current path, path style, and view transformation matrix.
func NewContext(r Renderer) *Context {
return &Context{r, &Path{}, DefaultStyle, nil, Identity, nil, Identity, nil}
}
// Width returns the width of the canvas.
func (c *Context) Width() float64 {
w, _ := c.Size()
return w
}
// Height returns the height of the canvas.
func (c *Context) Height() float64 {
_, h := c.Size()
return h
}
// Push saves the current draw state, so that it can be popped later on.
func (c *Context) Push() {
c.styleStack = append(c.styleStack, c.Style)
c.viewStack = append(c.viewStack, c.view)
c.coordViewStack = append(c.coordViewStack, c.coordView)
}
// 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.styleStack) == 0 {
return
}
c.Style = c.styleStack[len(c.styleStack)-1]
c.styleStack = c.styleStack[:len(c.styleStack)-1]
c.view = c.viewStack[len(c.viewStack)-1]
c.viewStack = c.viewStack[:len(c.viewStack)-1]
c.coordView = c.coordViewStack[len(c.coordViewStack)-1]
c.coordViewStack = c.coordViewStack[:len(c.coordViewStack)-1]
}
// SetCoordView sets the current affine transformation matrix through which all operation coordinates will be transformed.
func (c *Context) SetCoordView(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) {
w, h := c.Size()
switch coordSystem {
case CartesianI:
c.coordView = Identity
case CartesianII:
c.coordView = Identity.ReflectXAbout(w / 2.0)
case CartesianIII:
c.coordView = Identity.ReflectXAbout(w / 2.0).ReflectYAbout(h / 2.0)
case CartesianIV:
c.coordView = Identity.ReflectYAbout(h / 2.0)
}
}
// View returns the current affine transformation matrix.
func (c *Context) View() Matrix {
return c.view
}
// SetView sets the current affine transformation matrix through which all operations will be transformed.
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 one.
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.
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.
func (c *Context) ReflectYAbout(y float64) {
c.view = c.view.Mul(Identity.ReflectYAbout(y))
}
// Rotate rotates the view with rot in degrees.
func (c *Context) Rotate(rot float64) {
c.view = c.view.Mul(Identity.Rotate(rot))
}
// RotateAbout rotates the view 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()
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()
c.Style.StrokeColor = color.RGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)}
}
// SetStrokeWidth sets the width in mm for stroking operations.
func (c *Context) SetStrokeWidth(width float64) {
c.Style.StrokeWidth = width
}
// SetStrokeCapper sets the line cap function to be used for stroke endpoints.
func (c *Context) SetStrokeCapper(capper Capper) {
c.Style.StrokeCapper = capper
}
// SetStrokeJoiner sets the line join function to be used for stroke midpoints.
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 mm 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
}
// 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 the 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 for
// overlapping paths depend 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 end point.
// When sweep is true it means following the arc in a CCW direction in the Cartesian coordinate system, ie. that is CW in the upper-left coordinate system as is the case in SVGs.
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 applies) 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 (eg. 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 it.
func (c *Context) Fill() {
style := c.Style
style.StrokeColor = Transparent
c.RenderPath(c.path, style, c.view)
c.path = &Path{}
}
// Stroke strokes the current path and resets it.
func (c *Context) Stroke() {
style := c.Style
style.FillColor = Transparent
c.RenderPath(c.path, style, c.view)
c.path = &Path{}
}
// FillStroke fills and then strokes the current path and resets it.
func (c *Context) FillStroke() {
c.RenderPath(c.path, c.Style, c.view)
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.FillColor.A == 0 && (c.Style.StrokeColor.A == 0 || c.Style.StrokeWidth == 0.0) {
return
}
coord := c.coordView.Dot(Point{x, y})
m := 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. In particular, it only uses the current affine transformation matrix.
func (c *Context) DrawText(x, y float64, texts ...*Text) {
coord := 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 an image encoding (Lossy or Lossless) and DPM (dots-per-millimeter). A higher DPM will draw a smaller image.
func (c *Context) DrawImage(x, y float64, img image.Image, dpm float64) {
if img.Bounds().Size().Eq(image.Point{}) {
return
}
coord := c.coordView.Dot(Point{x, y})
m := c.view.Translate(coord.X, coord.Y).Scale(1.0/dpm, 1.0/dpm)
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 []layer
W, H float64
}
// New returns a new Canvas 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: []layer{},
W: width,
H: height,
}
}
// Size returns the size of the canvas in mm.
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 = append(c.layers, 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 = append(c.layers, 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 = append(c.layers, 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 = c.layers[:0]
}
// Fit shrinks the canvas size so all elements fit. The elements are translated towards the origin when any left/bottom margins exist and the canvas size is decreased if any margins exist. It will maintain a given margin.
func (c *Canvas) Fit(margin float64) {
if len(c.layers) == 0 {
c.W = 2 * margin
c.H = 2 * margin
return
}
rect := Rect{}
// TODO: slow when we have many paths (see Graph example)
for i, l := range c.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 i := range c.layers {
c.layers[i].m = Identity.Translate(-rect.X+margin, -rect.Y+margin).Mul(c.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()
}
for _, l := range c.layers {
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 (for the encoding).
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()
}