/
grid.go
324 lines (281 loc) · 8.37 KB
/
grid.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
package core
import (
"fmt"
"log/slog"
"math"
"strings"
"github.com/speedata/boxesandglue/backend/bag"
)
type area struct {
currentFrame int
name string
frame []*gridRect
}
func (area area) String() string {
var ret []string
for _, f := range area.frame {
ret = append(ret, f.String())
}
return fmt.Sprintf("%s: %s", area.name, strings.Join(ret, "|"))
}
// CurrentRow returns the current row of the current active frame.
func (area *area) CurrentRow() coord {
return area.frame[area.currentFrame].currentRow
}
// CurrentCol returns the current column of the current active frame.
func (area *area) CurrentCol() coord {
return area.frame[area.currentFrame].currentCol
}
// SetCurrentRow sets the current row in the active frame in the area.
func (area *area) SetCurrentRow(row coord) {
area.frame[area.currentFrame].currentRow = row
}
// SetCurrentCol sets the current column in the active frame in the area.
func (area *area) SetCurrentCol(col coord) {
area.frame[area.currentFrame].currentCol = col
}
type allocationMatrix map[gridCoord]int
type gridRect struct {
row coord
col coord
width coord
height coord
currentCol coord
currentRow coord
}
func (gr *gridRect) String() string {
return fmt.Sprintf("%d/%d wd: %d ht: %d cur: (%d,%d)", gr.col, gr.row, gr.width, gr.height, gr.currentCol, gr.currentRow)
}
func (am allocationMatrix) allocate(x, y coord) {
xy := newGridCoord(x, y)
am[xy]++
}
func (am allocationMatrix) allocValue(x, y coord) int {
return am[newGridCoord(x, y)]
}
func (am allocationMatrix) String() string {
var maxX, maxY coord
for k := range am {
x, y := k.XY()
if x > maxX {
maxX = x
}
if y > maxY {
maxY = y
}
}
var ret strings.Builder
for y := coord(1); y <= maxY; y++ {
for x := coord(1); x <= maxX; x++ {
if am.allocValue(x, y) > 0 {
ret.WriteRune('▊')
} else {
ret.WriteRune('.')
}
}
ret.WriteString("\n")
}
return ret.String()
}
type gridCoord int64
type coord int32
func (c coord) String() string {
return fmt.Sprintf("%d", c)
}
// newGridCoord creates a grid coordinate from the x and y values where (x,y) is
// the upper left. To be used in the allocation matrix.
func newGridCoord(x, y coord) gridCoord {
return gridCoord(x)<<32 + gridCoord(y)
}
// XY returns the x and the y coordinate.
func (gc gridCoord) XY() (coord, coord) {
return coord(gc >> 32), coord(gc & 0xffffffff)
}
func (gc gridCoord) GoString() string {
x, y := gc.XY()
return fmt.Sprintf("(%d,%d)", x, y)
}
func (gc gridCoord) String() string {
x, y := gc.XY()
return fmt.Sprintf("(%d,%d)", x, y)
}
type grid struct {
page *page
gridWidth bag.ScaledPoint // width of the grid cells
gridHeight bag.ScaledPoint // height of the grid cells
gridGapX bag.ScaledPoint // horizontal space between two grid cells
gridGapY bag.ScaledPoint // vertical space between two grid cells
nx int // nx * grid width + ( nx - 1 ) * grid gap + margin = paper width
ny int // ny * grid height + ( ny - 1 ) * grid gap + margin = paper height
marginLeft bag.ScaledPoint
marginRight bag.ScaledPoint
marginTop bag.ScaledPoint
marginBottom bag.ScaledPoint
allocatedBlocks allocationMatrix
areas map[string]*area
inGroup bool
}
func newGrid(xd *xtsDocument) *grid {
g := &grid{
gridWidth: xd.defaultGridWidth,
gridHeight: xd.defaultGridHeight,
gridGapX: xd.defaultGridGapX,
gridGapY: xd.defaultGridGapY,
areas: make(map[string]*area),
inGroup: true,
}
return g
}
// convenience function to get the current row of the area.
func (g *grid) currentRowArea(areaname string) coord {
if area, ok := g.areas[areaname]; ok {
return area.CurrentRow()
}
return 0
}
// convenience function to get the current column of the area.
func (g *grid) currentColArea(areaname string) coord {
if area, ok := g.areas[areaname]; ok {
return area.CurrentCol()
}
return 0
}
// Connect the grid to a page and initialize the allocation matrix.
func (g *grid) setPage(p *page) {
g.page = p
g.allocatedBlocks = make(allocationMatrix)
g.areas[pageAreaName] = &area{
name: pageAreaName,
frame: []*gridRect{{1, 1, coord(g.nx), coord(g.ny), 1, 1}},
}
}
func (g *grid) String() string {
return fmt.Sprintf("grid %dx%d", g.nx, g.ny)
}
// posX returns the horizontal offset relative to the left page border. Column 1
// returns the margin left.
func (g *grid) posX(column coord, area *area) bag.ScaledPoint {
offsetX := area.frame[area.currentFrame].col
posx := g.marginLeft + bag.ScaledPoint(column+offsetX-2)*g.gridWidth
if column > 1 {
posx += bag.ScaledPoint(column-2) * g.gridGapX
}
return posx
}
// posY returns the vertical offset relative to the top page border. Row 1
// returns the top margin.
func (g *grid) posY(row coord, area *area) bag.ScaledPoint {
offsetY := area.frame[area.currentFrame].row
posy := g.marginTop + bag.ScaledPoint(row+offsetY-2)*g.gridHeight
if row > 1 {
posy += bag.ScaledPoint(row-2) * g.gridGapY
}
return posy
}
// height returns the height of the number of columns.
func (g *grid) height(columns coord) bag.ScaledPoint {
return bag.ScaledPoint(columns)*g.gridHeight + bag.ScaledPoint(columns-1)*g.gridGapY
}
// width returns the width of the number of columns.
func (g *grid) width(columns coord) bag.ScaledPoint {
return bag.ScaledPoint(columns)*g.gridWidth + bag.ScaledPoint(columns-1)*g.gridGapX
}
func (g *grid) widthToColumns(width bag.ScaledPoint) coord {
r := float64(width) / float64(g.gridWidth+g.gridGapX)
return coord(math.Ceil(r - 0.005))
}
func (g *grid) heightToRows(height bag.ScaledPoint) coord {
r := float64(height) / float64(g.gridHeight+g.gridGapY)
return coord(math.Ceil(r - 0.005))
}
func (g *grid) allocate(x, y coord, area *area, wd, ht bag.ScaledPoint) {
var warningTopRaised, warningLeftRaised, warningRightRaised, warningBottomRaised bool
var offsetX coord
var offsetY coord
offsetX = area.frame[area.currentFrame].col
offsetY = area.frame[area.currentFrame].row
for col := coord(1); col <= g.widthToColumns(wd); col++ {
for row := coord(1); row <= g.heightToRows(ht); row++ {
if posX, posY := col+x+offsetX-2, row+y+offsetY-2; posX >= 1 && posY >= 1 && posX <= coord(g.nx) && posY <= coord(g.ny) {
g.allocatedBlocks.allocate(posX, posY)
} else {
if posX < 1 && !warningLeftRaised && !g.inGroup {
slog.Warn("object protrudes into the left margin")
warningLeftRaised = true
}
if posY < 1 && !warningTopRaised && !g.inGroup {
slog.Warn("object protrudes into the top margin")
warningTopRaised = true
}
if posX > coord(g.nx) && !warningRightRaised && !g.inGroup {
slog.Warn("object protrudes into the right margin")
warningRightRaised = true
}
if posY > coord(g.ny) && !warningBottomRaised && !g.inGroup {
slog.Warn("object protrudes into the bottom margin")
warningBottomRaised = true
}
}
}
}
col := x + g.widthToColumns(wd)
if col > coord(g.nx) {
area.SetCurrentCol(1)
area.SetCurrentRow(y + g.heightToRows(ht))
} else {
area.SetCurrentCol(col)
area.SetCurrentRow(y)
}
}
func (g *grid) findSuitableRow(wdCols coord, htRows coord, startColumn coord, area *area) coord {
frameMarginTop := area.frame[area.currentFrame].row - 1
areaHeight := area.frame[area.currentFrame].height
for row := area.CurrentRow() + frameMarginTop; row < areaHeight+frameMarginTop; row++ {
if row+htRows-1 > areaHeight {
break
}
fits := true
for r := row; r < row+htRows; r++ {
if !g.fitsInRow(startColumn, r, wdCols, area) {
fits = false
break
}
}
if fits {
return row
}
}
return -1
}
func (g *grid) nextRow(area *area) {
wd := area.frame[area.currentFrame].width
if area.CurrentCol() == 1 {
area.SetCurrentRow(area.CurrentRow() + 1)
}
r := g.findSuitableRow(wd, 1, 1, area)
if r == -1 {
g.nextArea(area)
r = 1
}
area.SetCurrentRow(r)
}
func (g *grid) fitsInRow(col coord, row coord, wdCols coord, area *area) bool {
col += area.frame[area.currentFrame].col - 1
row += area.frame[area.currentFrame].row - 1
for c := col; c < col+wdCols-1; c++ {
if g.allocatedBlocks.allocValue(c, row) > 0 {
return false
}
}
return true
}
// Go to the next area
func (g *grid) nextArea(area *area) {
currentFrameNumber := area.currentFrame
if currentFrameNumber+1 >= len(area.frame) {
clearPage(g.page.xd)
g.page.xd.setupPage()
return
}
area.currentFrame++
}