-
Notifications
You must be signed in to change notification settings - Fork 3
/
layout.lisp
393 lines (363 loc) · 15.1 KB
/
layout.lisp
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
(defun set-coords (node x0 y0 x1 y1)
(declare (ignorable node x0 y0 x1 y1))
(error ~"Unable to set coords"))
;; Setting the coordinates of a null, string or number node is a NOP
(defmethod set-coords (node x0 y0 x1 y1) (or (null? node)
(string? node)
(number? node))
(list x0 y0 x1 y1))
;; A callback node; used to inform someone of geometry computation
(defobject callback
(element f))
(defmethod set-coords (node x0 y0 x1 y1) (callback? node)
(node.f x0 y0 x1 y1)
(set-coords node.element x0 y0 x1 y1))
(defmacro callback (f element)
`(make-callback f: ,f element: ,element))
(defun callback (f element)
(new-callback element f))
;; An horizontal or vertical sequence of optionally spaced elements.
;; Each element can have a minimum and maximum size, and has a class
;; and a weight. Elements with higher class are given precedence in
;; space allocation, getting space in proportion to relative weights.
;; Spacing is inherited if not specified and affects all sub-elements.
(defvar *spacing* 0)
(defobject hv-element
(element
(min 0)
(max infinity)
(class 1)
(weight 100)))
(defobject hv
(elements ;; List of hv-element
(algorithm :H:) ;; or :V:
(spacing undefined) ;; spacing between elements
(border 0))) ;; uniform border
(defmethod add-element-method (node args) (hv? node)
(let ((i 0)
(min undefined)
(max undefined)
(class undefined)
(weight undefined))
(labels ((current () (aref args i))
(next () (aref args (1- (incf i)))))
(do () ((not (find (current) '(min: max: range: size: class: weight:))))
(case (next)
(min: (setf min (next)))
(max: (setf max (next)))
(range: (setf min (next)) (setf max (next)))
(size: (setf min (setf max (next))))
(class: (setf class (next)))
(weight: (setf weight (next)))))
(unless (= i (1- (length args)))
(error "A single element is expected"))
(push (new-hv-element (next) min max class weight)
node.elements))))
(defmethod set-coords (node x0 y0 x1 y1) (hv? node)
(let** ((epsilon 0.0001)
(elements node.elements)
(num-elements (length elements))
(spacing (if (number? node.spacing) node.spacing *spacing*))
(border node.border)
(algo node.algorithm)
(*spacing* spacing))
(if (= num-elements 0)
(list x0 y0 x1 y1)
(let** ((assigned (map (get min) elements))
(active (filter (lambda (i) (< (aref assigned i)
(aref elements i).max))
(range num-elements)))
(avail (- (if (= algo :H:)
(- x1 x0)
(- y1 y0))
(* 2 border)
(reduce #'+ assigned)
(* (1- num-elements) spacing))))
(do () ((or (zero? (length active))
(<= avail epsilon))
(if (= algo :H:)
(let ((x (+ border x0))
(mx x0)
(my y0))
(dolist ((w c) (zip assigned elements))
(let (((xa ya xb yb) (set-coords c.element
x (+ y0 border)
(+ x w) (- y1 border))))
(declare (ignorable xa ya))
(setf mx (max mx xb))
(setf my (max my yb)))
(incf x (+ w spacing)))
(list x0 y0 (+ mx border) (+ my border)))
(let ((y (+ border y0))
(mx x0)
(my y0))
(dolist ((h c) (zip assigned elements))
(let (((xa ya xb yb) (set-coords c.element
(+ x0 border) y
(- x1 border) (+ y h))))
(declare (ignorable xa ya))
(setf mx (max mx xb))
(setf my (max my yb)))
(incf y (+ h spacing)))
(list x0 y0 (+ mx border) (+ my border)))))
;;
;; Algorithm:
;;
;; First select the highest priority class among all active
;; nodes, then try to distribute the available space in
;; proportion to weights but not exceeding the maximum for a
;; given node. Finally remove saturated nodes from the
;; active list.
;;
;; At every step at least one node is saturated, or all
;; available space is distributed.
;;
;; IOW we're not going to loop forever (unless there's a
;; numeric precision problem, that's why epsilon is used).
;;
(let** ((minclass (apply #'min (map (lambda (i)
(aref elements i).class)
active)))
(selected (filter (lambda (i) (= (aref elements i).class
minclass))
active))
(selected-nodes (map (lambda (i) (aref elements i))
selected))
(total-weight (reduce #'+ (map (get weight)
selected-nodes)))
(share (/ avail total-weight)))
(dolist (i selected)
(let** ((n (aref elements i))
(quota (min (- n.max (aref assigned i))
(* share n.weight))))
(decf avail quota)
(incf (aref assigned i) quota)))
(setf active (filter (lambda (i)
(< (+ (aref assigned i) epsilon)
(aref elements i).max))
active))))))))
(defun hv-parser (algorithm args)
(let ((i 0)
(min undefined)
(max undefined)
(class undefined)
(weight undefined)
(spacing undefined)
(border undefined)
(elements (list)))
(labels ((current () (aref args i))
(next () (aref args (1- (incf i)))))
(do () ((not (find (current) '(spacing: border:))))
(case (next)
(spacing: (setf spacing (next)))
(border: (setf border (next)))))
(do () ((= i (length args)))
(case (current)
(min:
(next)
(setf max undefined)
(setf min (next)))
(max:
(next)
(setf min undefined)
(setf max (next)))
(range:
(next)
(setf min (next))
(setf max (next)))
(size:
(next)
(setf min (setf max (next))))
(class:
(next)
(setf weight undefined)
(setf min undefined)
(setf max undefined)
(setf class (next)))
(weight:
(next)
(setf min undefined)
(setf max undefined)
(setf weight (next)))
(:filler:
(next)
(push (new-hv-element null)
elements))
(otherwise
(when (keyword? (current))
(error ~"Invalid keyword {(current)} for H/V layout element"))
(push (new-hv-element (next)
min
max
class
weight)
elements))))
(new-hv elements
algorithm
spacing
border))))
(defun H (&rest args) (hv-parser :H: args))
(defun V (&rest args) (hv-parser :V: args))
;; A collection of elements each placed starting top-left and moving
;; to the right until an element doesn't fit, changing row when this
;; happens. Each element has a fixed width and height and there is an
;; optional horizontal and vertical spacing.
;;
;; If an element is too wide to be placed it will be placed anyway at
;; the beginning of its own line.
;;
;; [y1] parameter of a set-coords call to a flow node is ignored.
(defobject flow-element
(element
width ;; Fixed width
height)) ;; Fixed height
(defobject flow
(elements ;; List of flow-element
(h-spacing 0) ;; Space between elements on a row
(v-spacing 0))) ;; Space between rows
(defmethod add-element-method (node args) (flow? node)
(let ((i 0)
(width undefined)
(height undefined))
(labels ((current () (aref args i))
(next () (aref args (1- (incf i)))))
(do () ((not (find (current) '(width: height:))))
(case (next)
(width: (setf width (next)))
(height: (setf height (next)))))
(unless (= i (1- (length args)))
(error "A single element is expected"))
(push (new-flow-element (next) width height)
node.elements))))
(defmethod set-coords (node x0 y0 x1 y1) (flow? node)
(let ((x x0)
(y y0)
(row-height 0)
(max-x x0))
(dolist (c node.elements)
(when (and (> x x0)
(> (+ x c.width) x1))
(incf y (+ row-height node.v-spacing))
(setf row-height 0)
(setf x x0))
(set-coords c.element x y (+ x c.width) (+ y c.height))
(setf max-x (max max-x (+ x c.width)))
(setf row-height (max row-height c.height))
(incf x (+ c.width node.h-spacing)))
(list x0 y0 max-x (+ y row-height))))
(defun flow (&rest args)
(let ((i 0)
(width undefined)
(height undefined)
(h-spacing undefined)
(v-spacing undefined)
(elements (list)))
(labels ((current () (aref args i))
(next () (incf i) (aref args (1- i))))
(do () ((not (find (current) '(spacing: h-spacing: v-spacing:))))
(case (next)
(spacing: (setf h-spacing (setf v-spacing (next))))
(h-spacing: (setf h-spacing (next)))
(v-spacing: (setf v-spacing (next)))))
(do () ((= i (length args)))
(case (current)
(size: (next) (setf width (setf height (next))))
(width: (next) (setf width (next)))
(height: (next) (setf height (next)))
(otherwise
(when (keyword? (current))
(error ~"Invalid keyword {(current)} for flow layout element"))
(push (new-flow-element (next)
width
height)
elements))))
(new-flow elements
h-spacing
v-spacing))))
;; A bidimensional grid of elements. [rows] and [columns] are lists
;; of hv-element used to hold the parameters for row and column
;; position allocation.
(defobject tablayout
(elements ;; Bi-dimensional matrix
columns ;; hv of type :H: or number
rows)) ;; hv of type :V: or number
(defmethod set-coords (node x0 y0 x1 y1) (tablayout? node)
(let** ((col-pos (list))
(row-pos (list))
(columns node.columns)
(rows node.rows)
(elements node.elements)
(xa x0) (ya y0) (xb x1) (yb y1)
(drows (length elements))
(dcols (and drows (length (first elements)))))
(unless rows
(setf rows (V null)))
(unless columns
(setf columns (H null)))
(when (number? rows)
(setf rows (V size: rows null)))
(when (number? columns)
(setf columns (H size: columns null)))
;; Compute rows and columns layout
(let** ((nc (length columns.elements))
(h (make-hv elements: (map (lambda (i)
(let ((c (aref columns.elements (min i (1- nc)))))
(make-hv-element
element: (new-callback
null
(lambda (x0 y0 x1 y1)
(declare (ignorable y0 y1))
(push (list x0 x1) col-pos)))
min: c.min
max: c.max
class: c.class
weight: c.weight)))
(range dcols))
algorithm: :H:
spacing: columns.spacing))
(nr (length rows.elements))
(v (make-hv elements: (map (lambda (i)
(let ((c (aref rows.elements (min i (1- nr)))))
(make-hv-element
element: (new-callback
null
(lambda (x0 y0 x1 y1)
(declare (ignorable x0 x1))
(push (list y0 y1) row-pos)))
min: c.min
max: c.max
class: c.class
weight: c.weight)))
(range drows))
algorithm: :V:
spacing: rows.spacing)))
(dolist (L (list h v))
(let (((xxa yya xxb yyb) (set-coords L x0 y0 x1 y1)))
(setf xa (min xa xxa))
(setf ya (min ya yya))
(setf xb (max xb xxb))
(setf yb (max yb yyb))))
;; Fix elements and return extension
(enumerate (row (ya yb) row-pos)
(enumerate (col (xa xb) col-pos)
(set-coords (aref elements row col) xa ya xb yb)))
(list xa ya xb yb))))
(defun tablayout (&rest args)
(let ((i 0)
(columns undefined)
(rows undefined))
(labels ((current () (aref args i))
(next () (incf i) (aref args (1- i))))
(do () ((not (find (current) '(columns: rows:))))
(case (next)
(columns: (setf columns (next)))
(rows: (setf rows (next)))))
(unless (= i (1- (length args)))
(error "Tablayout needs exactly one data element"))
(new-tablayout (next)
columns
rows))))
(defun add-element (node &rest args)
(add-element-method node args))
(export set-coords add-element
callback flow H V tablayout)