-
Notifications
You must be signed in to change notification settings - Fork 13
/
gridder.go
320 lines (275 loc) · 8.03 KB
/
gridder.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
package gridder
import (
"errors"
"image/color"
"io"
"github.com/fogleman/gg"
"golang.org/x/image/font"
)
var (
errNoRows = errors.New("no rows provided")
errNoColumns = errors.New("no columns provided")
errOutOfBounds = errors.New("out of bounds")
)
// New creates a new gridder and sets it up with its configuration
func New(imageConfig ImageConfig, gridConfig GridConfig) (*Gridder, error) {
rows := gridConfig.GetRows()
if rows == 0 {
return nil, errNoRows
}
columns := gridConfig.GetColumns()
if columns == 0 {
return nil, errNoColumns
}
gridder := Gridder{
imageConfig: imageConfig,
gridConfig: gridConfig,
ctx: gg.NewContext(imageConfig.GetWidth(), imageConfig.GetHeight()),
}
gridder.paintBackground()
return &gridder, nil
}
// Gridder gridder structure
type Gridder struct {
imageConfig ImageConfig
gridConfig GridConfig
ctx *gg.Context
}
// SavePNG saves to PNG
func (g *Gridder) SavePNG() error {
g.paintGrid()
g.paintBorder()
return g.ctx.SavePNG(g.imageConfig.GetName())
}
// EncodePNG encodes the image as a PNG and writes it to the provided io.Writer.
func (g *Gridder) EncodePNG(w io.Writer) error {
g.paintGrid()
g.paintBorder()
return g.ctx.EncodePNG(w)
}
// PaintCell paints Cell
func (g *Gridder) PaintCell(row int, column int, color color.Color) error {
err := g.verifyInBounds(row, column)
if err != nil {
return err
}
cellWidth, cellHeight := g.getCellDimensions()
paintWidth := cellWidth - g.gridConfig.GetLineStrokeWidth()
paintHeight := cellHeight - g.gridConfig.GetLineStrokeWidth()
return g.DrawRectangle(row, column, RectangleConfig{Width: paintWidth, Height: paintHeight, Color: color})
}
// DrawRectangle draws a rectangle in a cell
func (g *Gridder) DrawRectangle(row int, column int, rectangleConfigs ...RectangleConfig) error {
err := g.verifyInBounds(row, column)
if err != nil {
return err
}
center := g.getCellCenter(row, column)
rectangleConfig := getFirstRectangleConfig(rectangleConfigs...)
rectangleWidth := rectangleConfig.GetWidth()
rectangleHeight := rectangleConfig.GetHeight()
x := center.X - rectangleWidth/2
y := center.Y - rectangleHeight/2
g.ctx.Push()
dashes := rectangleConfig.GetDashes()
if dashes > 0 {
g.ctx.SetDash(dashes)
} else {
g.ctx.SetDash()
}
g.ctx.RotateAbout(gg.Radians(rectangleConfig.GetRotate()), center.X, center.Y)
g.ctx.DrawRectangle(x, y, rectangleWidth, rectangleHeight)
g.ctx.SetLineWidth(rectangleConfig.GetStrokeWidth())
g.ctx.SetColor(rectangleConfig.GetColor())
if rectangleConfig.IsStroke() {
g.ctx.Stroke()
} else {
g.ctx.Fill()
}
g.ctx.Pop()
return nil
}
// DrawCircle draws a circle in a cell
func (g *Gridder) DrawCircle(row int, column int, circleConfigs ...CircleConfig) error {
err := g.verifyInBounds(row, column)
if err != nil {
return err
}
center := g.getCellCenter(row, column)
circleConfig := getFirstCircleConfig(circleConfigs...)
g.ctx.Push()
dashes := circleConfig.GetDashes()
if dashes > 0 {
g.ctx.SetDash(dashes)
} else {
g.ctx.SetDash()
}
g.ctx.DrawPoint(center.X, center.Y, circleConfig.GetRadius())
g.ctx.SetLineWidth(circleConfig.GetStrokeWidth())
g.ctx.SetColor(circleConfig.GetColor())
if circleConfig.IsStroke() {
g.ctx.Stroke()
} else {
g.ctx.Fill()
}
g.ctx.Pop()
return nil
}
// DrawPath draws a path between two cells
func (g *Gridder) DrawPath(row1 int, column1 int, row2 int, column2 int, pathConfigs ...PathConfig) error {
err := g.verifyInBounds(row1, column1)
if err != nil {
return err
}
err = g.verifyInBounds(row2, column2)
if err != nil {
return err
}
center1 := g.getCellCenter(row1, column1)
center2 := g.getCellCenter(row2, column2)
pathConfig := getFirstPathConfig(pathConfigs...)
g.ctx.Push()
dashes := pathConfig.GetDashes()
if dashes > 0 {
g.ctx.SetDash(dashes)
} else {
g.ctx.SetDash()
}
g.ctx.SetColor(pathConfig.GetColor())
g.ctx.SetLineWidth(pathConfig.GetStrokeWidth())
g.ctx.DrawLine(center1.X, center1.Y, center2.X, center2.Y)
g.ctx.Stroke()
g.ctx.Pop()
return nil
}
// DrawLine draws a line in a cell
func (g *Gridder) DrawLine(row int, column int, lineConfigs ...LineConfig) error {
err := g.verifyInBounds(row, column)
if err != nil {
return err
}
center := g.getCellCenter(row, column)
lineConfig := getFirstLineConfig(lineConfigs...)
length := lineConfig.GetLength()
x1 := center.X - length/2
x2 := center.X + length/2
y := center.Y
g.ctx.Push()
dashes := lineConfig.GetDashes()
if dashes > 0 {
g.ctx.SetDash(dashes)
} else {
g.ctx.SetDash()
}
g.ctx.RotateAbout(gg.Radians(lineConfig.GetRotate()), center.X, center.Y)
g.ctx.DrawLine(x1, y, x2, y)
g.ctx.SetLineWidth(lineConfig.GetStrokeWidth())
g.ctx.SetColor(lineConfig.GetColor())
g.ctx.Stroke()
g.ctx.Pop()
return nil
}
// DrawString draws a string in a cell
func (g *Gridder) DrawString(row int, column int, text string, fontFace font.Face, stringConfigs ...StringConfig) error {
err := g.verifyInBounds(row, column)
if err != nil {
return err
}
center := g.getCellCenter(row, column)
stringConfig := getFirstStringConfig(stringConfigs...)
g.ctx.Push()
g.ctx.SetFontFace(fontFace)
g.ctx.SetColor(stringConfig.GetColor())
g.ctx.RotateAbout(gg.Radians(stringConfig.GetRotate()), center.X, center.Y)
g.ctx.DrawStringAnchored(text, center.X, center.Y, 0.5, 0.35)
g.ctx.Pop()
return nil
}
func (g *Gridder) paintBackground() {
margin := float64(g.gridConfig.GetMarginWidth())
g.ctx.Translate(margin, margin)
g.ctx.SetColor(g.gridConfig.GetBackgroundColor())
g.ctx.Clear()
}
func (g *Gridder) paintGrid() {
canvasWidth, canvasHeight := g.getGridDimensions()
cellWidth, cellHeight := g.getCellDimensions()
columns := float64(g.gridConfig.GetColumns())
g.ctx.Push()
for i := 1.0; i < columns; i++ {
x := i * cellWidth
g.ctx.MoveTo(x, 0)
g.ctx.LineTo(x, canvasHeight)
}
rows := float64(g.gridConfig.GetRows())
for i := 1.0; i < rows; i++ {
y := i * cellHeight
g.ctx.MoveTo(0, y)
g.ctx.LineTo(canvasWidth, y)
}
dashes := g.gridConfig.GetLineDashes()
if dashes > 0 {
g.ctx.SetDash(dashes)
} else {
g.ctx.SetDash()
}
g.ctx.SetColor(g.gridConfig.GetLineColor())
g.ctx.SetLineWidth(g.gridConfig.GetLineStrokeWidth())
g.ctx.Stroke()
g.ctx.Pop()
}
func (g *Gridder) paintBorder() {
canvasWidth, canvasHeight := g.getGridDimensions()
cellWidth, cellHeight := g.getCellDimensions()
columns := float64(g.gridConfig.GetColumns())
g.ctx.Push()
g.ctx.MoveTo(0, 0)
g.ctx.LineTo(0, canvasHeight)
g.ctx.MoveTo(cellWidth*columns, 0)
g.ctx.LineTo(cellWidth*columns, canvasHeight)
rows := float64(g.gridConfig.GetRows())
g.ctx.MoveTo(0, 0)
g.ctx.LineTo(canvasWidth, 0)
g.ctx.MoveTo(0, cellHeight*rows)
g.ctx.LineTo(canvasWidth, cellHeight*rows)
dashes := g.gridConfig.GetBorderDashes()
if dashes > 0 {
g.ctx.SetDash(dashes)
} else {
g.ctx.SetDash()
}
g.ctx.SetLineWidth(g.gridConfig.GetBorderStrokeWidth())
g.ctx.SetColor(g.gridConfig.GetBorderColor())
g.ctx.Stroke()
g.ctx.Pop()
}
func (g *Gridder) getCellDimensions() (float64, float64) {
gridWidth, gridHeight := g.getGridDimensions()
cellWidth := gridWidth / float64(g.gridConfig.GetColumns())
cellHeight := gridHeight / float64(g.gridConfig.GetRows())
return cellWidth, cellHeight
}
func (g *Gridder) getGridDimensions() (float64, float64) {
imageWidth := g.imageConfig.GetWidth()
imageHeight := g.imageConfig.GetHeight()
gridWidth := float64(g.gridConfig.GetWidth(imageWidth))
gridHeight := float64(g.gridConfig.GetHeight(imageHeight))
return gridWidth, gridHeight
}
func (g *Gridder) getCellCenter(row, column int) *gg.Point {
columns := float64(g.gridConfig.GetColumns())
rows := float64(g.gridConfig.GetRows())
cellWidth, cellHeight := g.getCellDimensions()
gridWidth, gridHeight := g.getGridDimensions()
x := float64(column)*(gridWidth/columns) + cellWidth/2
y := float64(row)*(gridHeight/rows) + cellHeight/2
return &gg.Point{X: x, Y: y}
}
func (g *Gridder) verifyInBounds(row, column int) error {
columns := g.gridConfig.GetColumns()
rows := g.gridConfig.GetRows()
if row < 0 || row >= rows || column < 0 || column >= columns {
return errOutOfBounds
}
return nil
}