-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrob.go
413 lines (357 loc) · 9.74 KB
/
grob.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
package plot
import (
"fmt"
"image/color"
"math"
"strings"
"gonum.org/v1/plot/vg"
)
type Grob interface {
Draw(vp Viewport)
String() string
}
// -------------------------------------------------------------------------
// Grob Point
type GrobPoint struct {
x, y float64
size float64
shape PointShape
color color.Color
}
var _ Grob = GrobPoint{}
func (point GrobPoint) Draw(vp Viewport) {
vp.Canvas.Push()
vp.Canvas.SetColor(point.color)
vp.Canvas.SetLineWidth(1)
vp.Canvas.SetLineDash(nil, 0)
x, y := vp.X(point.x), vp.Y(point.y)
s := vg.Points(point.size)
var p vg.Path
draw := vp.Canvas.Stroke
if point.shape >= SolidCirclePoint && point.shape <= SolidNablaPoint {
draw = vp.Canvas.Fill
}
switch point.shape {
case BlankPoint:
return
case DotPoint:
// dpi := vp.Canvas.DPI()
p.Arc(vg.Point{x, y}, 1, 0, 2*math.Pi)
p.Close()
vp.Canvas.Fill(p)
case CirclePoint, SolidCirclePoint:
p.Arc(vg.Point{x, y}, vg.Points(point.size), 0, 2*math.Pi)
p.Close()
draw(p)
case SquarePoint, SolidSquarePoint:
p.Move(vg.Point{x - s, y - s})
p.Line(vg.Point{x + s, y - s})
p.Line(vg.Point{x + s, y + s})
p.Line(vg.Point{x - s, y + s})
p.Close()
draw(p)
case DiamondPoint, SolidDiamondPoint:
p.Move(vg.Point{x, y - s})
p.Line(vg.Point{x + s, y})
p.Line(vg.Point{x, y + s})
p.Line(vg.Point{x - s, y})
p.Close()
draw(p)
case DeltaPoint, SolidDeltaPoint:
ss := 0.57735 * s
p.Move(vg.Point{x, y + 2*ss})
p.Line(vg.Point{x - s, y - ss})
p.Line(vg.Point{x + s, y - ss})
p.Close()
draw(p)
case NablaPoint, SolidNablaPoint:
ss := 0.57735 * s
p.Move(vg.Point{x, y - 2*ss})
p.Line(vg.Point{x - s, y + ss})
p.Line(vg.Point{x + s, y + ss})
p.Close()
draw(p)
case CrossPoint:
ss := s / 1.3
p.Move(vg.Point{x - ss, y - ss})
p.Line(vg.Point{x + ss, y + ss})
p.Move(vg.Point{x - ss, y + ss})
p.Line(vg.Point{x + ss, y - ss})
draw(p)
case PlusPoint:
p.Move(vg.Point{x - s, y})
p.Line(vg.Point{x + s, y})
p.Move(vg.Point{x, y - s})
p.Line(vg.Point{x, y + s})
draw(p)
case StarPoint:
ss := s / 1.3
p.Move(vg.Point{x - ss, y - ss})
p.Line(vg.Point{x + ss, y + ss})
p.Move(vg.Point{x - ss, y + ss})
p.Line(vg.Point{x + ss, y - ss})
p.Move(vg.Point{x - s, y})
p.Line(vg.Point{x + s, y})
p.Move(vg.Point{x, y - s})
p.Line(vg.Point{x, y + s})
draw(p)
default:
println("Implement Draw for points " + point.shape.String())
}
vp.Canvas.Pop()
}
func (point GrobPoint) String() string {
return fmt.Sprintf("Point(%.3f,%.3f %s %s %.1f)",
point.x, point.y, Color2String(point.color),
point.shape.String(), point.size)
}
// -------------------------------------------------------------------------
// Grob Line
type GrobLine struct {
x0, y0, x1, y1 float64
size float64
linetype LineType
color color.Color
}
var _ Grob = GrobLine{}
var dashLength = [][]vg.Length{
[]vg.Length{},
[]vg.Length{},
[]vg.Length{10},
[]vg.Length{10, 8},
[]vg.Length{4, 4},
[]vg.Length{10, 4, 4, 4},
[]vg.Length{10, 3},
[]vg.Length{10, 2, 4, 2},
}
func (line GrobLine) Draw(vp Viewport) {
vp.Canvas.SetColor(line.color)
vp.Canvas.SetLineWidth(vg.Points(line.size))
vp.Canvas.SetLineDash(dashLength[line.linetype%7], 0)
fmt.Println("%%%%%%%%%%%%", line.linetype, line.linetype%7, dashLength[line.linetype%7])
x0, y0 := vp.X(line.x0), vp.Y(line.y0)
x1, y1 := vp.X(line.x1), vp.Y(line.y1)
var p vg.Path
p.Move(vg.Point{x0, y0})
p.Line(vg.Point{x1, y1})
vp.Canvas.Stroke(p)
}
func (line GrobLine) String() string {
return fmt.Sprintf("Line(%.3f,%.3f - %.3f,%.3f %s %s %.1f)",
line.x0, line.y0, line.x1, line.y1,
Color2String(line.color), line.linetype.String(),
line.size)
}
// -------------------------------------------------------------------------
// Grob Path
type GrobPath struct {
points []struct{ x, y float64 }
size float64
linetype LineType
color color.Color
}
var _ Grob = GrobPath{}
func (path GrobPath) Draw(vp Viewport) {
vp.Canvas.Push()
vp.Canvas.SetColor(path.color)
vp.Canvas.SetLineWidth(vg.Points(path.size))
vp.Canvas.SetLineDash(dashLength[path.linetype], 0)
x, y := vp.X(path.points[0].x), vp.Y(path.points[0].y)
var p vg.Path
p.Move(vg.Point{x, y})
for i := 1; i < len(path.points); i++ {
x, y = vp.X(path.points[i].x), vp.Y(path.points[i].y)
p.Line(vg.Point{x, y})
}
vp.Canvas.Stroke(p)
vp.Canvas.Pop()
}
func (path GrobPath) String() string {
// Pretty print points
ppp := func(points []struct{ x, y float64 }) string {
s := []string{}
for _, p := range points {
s = append(s, fmt.Sprintf("%.2f,%.2f", p.x, p.y))
}
return strings.Join(s, " - ")
}
var points string
if n := len(path.points); n <= 6 {
points = ppp(path.points)
} else {
points = ppp(path.points[0:3]) + " ... " + ppp(path.points[n-3:n])
}
return fmt.Sprintf("Path(%s %s %s %.1f)",
points, Color2String(path.color), path.linetype.String(),
path.size)
}
// -------------------------------------------------------------------------
// Grob Text
type GrobText struct {
x, y float64
text string
size float64
color color.Color
angle float64
font string
vjust, hjust float64
}
var _ Grob = GrobText{}
func (text GrobText) Font() vg.Font {
fname := text.font
if fname == "" {
fname = "Courier-Bold"
}
font, err := vg.MakeFont(fname, vg.Length(text.size))
if err != nil {
panic(err.Error())
}
return font
}
func (text GrobText) Draw(vp Viewport) {
vp.Canvas.Push()
vp.Canvas.SetColor(text.color)
x, y := vp.X(text.x), vp.Y(text.y)
font := text.Font()
ww, hh := text.BoundingBox()
// w := font.Width(text.text)
h := font.Extents().Ascent
dx := ww * vg.Length(text.hjust)
dy := hh * vg.Length(text.vjust)
if text.angle <= math.Pi/2 {
dx -= h * vg.Length(math.Sin(text.angle))
} else if text.angle <= math.Pi {
dx -= ww
dy += h * vg.Length(math.Cos(text.angle))
} else {
panic("Implement me....")
}
vp.Canvas.Translate(vg.Point{x - dx, y - dy})
vp.Canvas.Rotate(text.angle)
vp.Canvas.FillString(font, vg.Point{0, 0}, text.text)
// fmt.Printf("Printed %s %.1f %.1f\n", text.String(), ww, hh)
vp.Canvas.Pop()
}
func (text GrobText) BoundingBox() (vg.Length, vg.Length) {
font := text.Font()
// Compute width ww and height hh of the rotateted bounding box.
w := font.Width(text.text)
h := font.Extents().Ascent
s := math.Sin(text.angle)
z := vg.Length(math.Sqrt(1 - s*s))
ww := w*z + h*vg.Length(s)
hh := w*vg.Length(s) + h*z
return ww, hh
}
func (text GrobText) String() string {
fnt := fmt.Sprintf("%s/%.0fpt", text.font, text.size)
return fmt.Sprintf("Text(%.3f,%.3f %q %s %.0f° %.1f/%.1f %q)",
text.x, text.y, text.text, Color2String(text.color),
180*text.angle/math.Pi, text.hjust, text.vjust, fnt)
}
// -------------------------------------------------------------------------
// Grob Rect
type GrobRect struct {
xmin, ymin float64
xmax, ymax float64
fill color.Color
}
var _ Grob = GrobRect{}
func (rect GrobRect) Draw(vp Viewport) {
println("GrobRect.Draw: ", rect.String(), " to ", vp.String())
vp.Canvas.Push()
vp.Canvas.SetColor(rect.fill)
vp.Canvas.SetLineWidth(2)
vp.Canvas.SetLineDash(nil, 0)
xmin, ymin := vp.X(rect.xmin), vp.Y(rect.ymin)
xmax, ymax := vp.X(rect.xmax), vp.Y(rect.ymax)
println(xmin, ymin, " -- ", xmax, ymax)
var p vg.Path
p.Move(vg.Point{xmin, ymin})
p.Line(vg.Point{xmax, ymin})
p.Line(vg.Point{xmax, ymax})
p.Line(vg.Point{xmin, ymax})
p.Close()
vp.Canvas.Fill(p)
vp.Canvas.Pop()
}
func (rect GrobRect) String() string {
return fmt.Sprintf("Rect(%.3f,%.3f - %.3f,%.3f %s)",
rect.xmin, rect.ymin, rect.xmax, rect.ymax,
Color2String(rect.fill))
}
// -------------------------------------------------------------------------
// Grob Group
type GrobGroup struct {
x0, y0 float64
elements []Grob
}
var _ Grob = GrobGroup{}
func (group GrobGroup) Draw(vp Viewport) {
// x0, y0 := vp.X(group.x0), vp.Y(group.y0)
// fmt.Printf("Guides translate %.1f %.1f\n", x0,y0)
vp.Canvas.Push()
vp.Canvas.Translate(vg.Point{vg.Length(group.x0), vg.Length(group.y0)})
for _, g := range group.elements {
g.Draw(vp)
}
vp.Canvas.Pop()
}
func (group GrobGroup) String() string {
return fmt.Sprintf("Group of %d", len(group.elements))
}
// -------------------------------------------------------------------------
// Viewport
type Viewport struct {
Canvas vg.Canvas
// All coordinates and widths/heights in canvas units.
X0, Y0 vg.Length // The lower left corner of this vp
Width, Height vg.Length // The width and height of this vp
// Direct to true interpretes geom coordinates a direct
// length and uses them unscaled.
Direct bool
}
func (vp Viewport) String() string {
return fmt.Sprintf("%.2f x %.2f + %.2f + %.2f (PS points)",
vp.X0.Points(), vp.Y0.Points(), vp.Width.Points(), vp.Height.Points())
}
// SubViewport returns the area described by x0,y0,width,height in
// natural grob coordinates [0,1] as a viewport.
func (vp Viewport) Sub(x0, y0, width, height float64) Viewport {
sub := Viewport{
X0: vp.X0 + vg.Length(x0)*vp.Width,
Y0: vp.Y0 + vg.Length(y0)*vp.Height,
Width: vg.Length(width) * vp.Width,
Height: vg.Length(height) * vp.Height,
Canvas: vp.Canvas,
}
return sub
}
// X and Y turn natural grob coordinates [0,1] to canvas lengths.
func (vp Viewport) X(x float64) vg.Length {
ans := vp.X0
if !vp.Direct {
ans += vg.Length(x) * vp.Width
} else {
ans += vg.Length(x)
}
// fmt.Printf("X( %.3f ) = %.1fin\n", x, ans.Inches())
return ans
}
func (vp Viewport) Y(y float64) vg.Length {
ans := vp.Y0
if !vp.Direct {
ans += vg.Length(y) * vp.Height
} else {
ans += vg.Length(y)
}
// fmt.Printf("Y( %.3f ) = %.1fin\n", y, ans.Inches())
return ans
}
// XI and YI turn canvas length to natural grob factors.
func (vp Viewport) XI(w vg.Length) float64 {
return float64(w / vp.Width)
}
func (vp Viewport) YI(h vg.Length) float64 {
return float64(h / vp.Height)
}