forked from jneug/typst-finite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout.typ
517 lines (455 loc) · 13.5 KB
/
layout.typ
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
510
511
512
513
514
515
516
517
#import "util.typ": *
// Apply element styles to the context. (e.g. from set-style)
#let apply-style(ctx, element) = {
if "style" in element {
ctx.style = styles.resolve(
ctx.style,
if type(element.style) == "function" {
(element.style)(ctx)
} else {
element.style
}
)
}
return ctx
}
// Resolve radii for states by applying styles from other elements.
#let resolve-radii(ctx, elements) = {
let radii = (:)
for element in elements {
ctx = apply-style(ctx, element)
if "name" in element and "radius" in element {
radii.insert(element.name, (element.radius)(ctx))
}
}
return radii
}
// A base element for creating layouts.
#let base(position, name, anchor, body, children:()) = (
name: name,
coordinates: (position,),
anchor: anchor,
default-anchor: "origin",
custom-anchors-ctx: (ctx, pos) => {
let anchors = ctx.groups.last().anchors
for (k,v) in anchors {
anchors.insert(k, vector.add(translate, a))
}
anchors.insert("origin", pos)
return anchors
},
before: (ctx) => {
ctx.groups.push((
ctx: ctx,
anchors: (:),
nodes: body.map((e) => e.at("name", default:none)).filter(is.not-none)
))
return ctx
},
push-transform: (ctx) => {
let t = vector.sub(
util.apply-transform(ctx.transform, coordinate.resolve(ctx, position)),
util.apply-transform(ctx.transform, (0,0,0))
)
return matrix.mul-mat(matrix.transform-translate(..t), ctx.transform)
},
after: (ctx, pos) => {
let node = ctx.nodes.at(name)
let anchor = if is.n(anchor) {"left"} else {anchor}
let translate = vector.sub(node.anchors.default, node.anchors.at(anchor))
let self = ctx.groups.pop()
let nodes = ctx.nodes
ctx = self.ctx
ctx.nodes.insert(name, nodes.at(name))
for name in self.nodes {
if name in nodes {
let node = nodes.at(name)
for (k, a) in node.anchors {
node.anchors.insert(k, vector.add(translate, a))
}
ctx.nodes.insert(name, node)
}
}
ctx.prev.pt = pos
return prepare-ctx(ctx, force:true)
},
children: children
)
/// Arrange states in a line.
///
/// The direction of the line can be set via #arg[dir] either to an #dtype("alignment")
/// or a `vector` with a x and y shift.
///
/// #example(breakable:true)[```
/// #let aut = range(6).fold((:), (d, s) => {d.insert("q"+str(s), none); d})
/// #finite.automaton(
/// aut,
/// initial: none, final: none,
/// layout:finite.layout.linear.with(dir: right)
/// )
/// #finite.automaton(
/// aut,
/// initial: none, final: none,
/// layout:finite.layout.linear.with(dir:(.5, -.2))
/// )
/// ```]
///
/// - position (coordinate): Position of the anchor point.
/// - dir (vector,alignment,2d alignment): Direction of the line.
/// - spacing (float): Spacing between states on the line.
/// - name (string): Name for the element to access later.
/// - anchor (string): Name of the anchor to use for the layout.
/// - body (array): Array of CETZ elements to draw.
#let linear(
position, name: none, anchor: "left",
dir: right,
spacing: .6,
body
) = {
if is.any-type("alignment", "2d alignment", dir) {
dir = vector.scale(align-to-vec(dir), spacing)
}
dir = vector.norm(dir)
if is.n(name) {
name = "layout" + body.map((e) => e.at("name", default:"")).join("-")
}
let base = base(position, name, anchor, body)
base.children = (ctx) => {
let elements = ()
let at = (0,0)
let spacing = vector.scale(dir, spacing)
for element in body {
ctx = apply-style(ctx, element)
if "name" in element and "radius" in element {
let r = vector.scale(dir, (element.radius)(ctx))
if at != (0,0) {
at = vector.add(at, r)
}
element.coordinates = (at,)
at = vector.add(at, vector.add(r, spacing))
}
elements.push(element)
}
elements
}
return (base,)
}
/// Arrange states in a circle.
///
/// #example(breakable:true)[```
/// #let aut = range(6).fold((:), (d, s) => {d.insert("q"+str(s), none); d})
/// #grid(columns: 2, gutter: 2em,
/// finite.automaton(
/// aut,
/// initial: none, final: none,
/// layout:finite.layout.circular,
/// style: (q0: (fill: yellow.lighten(60%)))
/// ),
/// finite.automaton(
/// aut,
/// initial: none, final: none,
/// layout:finite.layout.circular.with(offset:45deg),
/// style: (q0: (fill: yellow.lighten(60%)))
/// ),
/// finite.automaton(
/// aut,
/// initial: none, final: none,
/// layout:finite.layout.circular.with(dir:left),
/// style: (q0: (fill: yellow.lighten(60%)))
/// ),
/// finite.automaton(
/// aut,
/// initial: none, final: none,
/// layout:finite.layout.circular.with(dir:left, offset:45deg),
/// style: (q0: (fill: yellow.lighten(60%)))
/// )
/// )
/// ```]
///
/// - position (coordinate): Position of the anchor point.
/// - dir (alignment): Direction of the circle. Either #value(left) or #value(right).
/// - spacing (float): Spacing between states on the line.
/// - radius (float,auto): Either a fixed radius or #value(auto) to calculate a suitable the radius.
/// - offset (angle): An offset angle to place the first state at.
/// - name (string): Name for the element to access later.
/// - anchor (string): Name of the anchor to use for the layout.
/// - body (array): Array of CETZ elements to draw.
#let circular(
position, name: none, anchor: "left",
dir: right,
spacing: .6,
radius: auto,
offset: 0deg,
body
) = {
if is.n(name) {
name = "layout" + body.map((e) => e.at("name", default:"")).join("-")
}
let layout = base(position, name, anchor, body)
layout.children = (ctx) => {
let elements = ()
let radii = resolve-radii(ctx, body)
let n = radii.len()
let len = radii.values().fold(0, (s, r) => s + 2 * r + spacing)
let radius = radius
if is.a(radius) {
radius = len / (2*calc.pi)
}
let at = -radii.values().first()
let last = none
let last-radius = 0
for element in body {
ctx = apply-style(ctx, element)
if "name" in element and "radius" in element {
let r = radii.at(element.name)
let ang = offset + math.map(
0.0, len,
0deg, 360deg,
at + r
)
element.coordinates = ((
radius - radius * calc.cos(ang),
if dir == right {radius} else {-radius} * calc.sin(ang),
),)
at += 2 * r + spacing
}
elements.push(element)
}
elements
}
return (layout,)
}
/// Arrange states in rows and columns.
///
/// #example[```
/// #let aut = range(6).fold((:), (d, s) => {d.insert("q"+str(s), none); d})
/// #finite.automaton(
/// aut,
/// initial: none, final: none,
/// layout:finite.layout.grid.with(columns:3)
/// )
/// ```]
///
/// - position (coordinate): Position of the anchor point.
/// - columns (integer): Number of columns per row.
/// - spacing (float): Spacing between states on the grid.
/// - name (string): Name for the element to access later.
/// - anchor (string): Name of the anchor to use for the layout.
/// - body (array): Array of CETZ elements to draw.
#let grid(
position, name: none, anchor: "left",
columns: 4,
spacing: .6,
body
) = {
if not is.arr(spacing) {
spacing = (x: spacing, y: spacing)
} else {
spacing = (x: spacing.first(), y: spacing.last())
}
if is.n(name) {
name = "layout" + body.map((e) => e.at("name", default:"")).join("-")
}
let layout = base(position, name, anchor, body)
layout.children = (ctx) => {
let elements = ()
let radii = resolve-radii(ctx, body)
let max-radius = calc.max(..radii.values())
let last = none
let i = 0
for element in body {
ctx = apply-style(ctx, element)
if "name" in element and "radius" in element {
let (row, col) = (
calc.quo(i, columns),
calc.rem(i, columns)
)
element.coordinates = ((
col * (2*max-radius + spacing.x),
row * (2*max-radius + spacing.y)
),)
i += 1
}
elements.push(element)
}
return elements
}
return (layout,)
}
/// Arrange states in a grid, but alternate the direction in every even and odd row.
///
/// #example(breakable:true)[```
/// #let aut = range(6).fold((:), (d, s) => {d.insert("q"+str(s), none); d})
/// #finite.automaton(
/// aut,
/// initial: none, final: none,
/// layout:finite.layout.snake.with(columns:3)
/// )
/// ```]
///
/// - position (coordinate): Position of the anchor point.
/// - columns (integer): Number of columns per row.
/// - spacing (float): Spacing between states on the line.
/// - name (string): Name for the element to access later.
/// - anchor (string): Name of the anchor to use for the layout.
/// - body (array): Array of CETZ elements to draw.
#let snake(
position, name: none, anchor: "left",
columns: 4,
spacing: .6,
body
) = {
if not is.arr(spacing) {
spacing = (x: spacing, y: spacing)
} else {
spacing = (x: spacing.first(), y: spacing.last())
}
if is.n(name) {
name = "layout" + body.map((e) => e.at("name", default:"")).join("-")
}
let layout = base(position, name, anchor, body)
layout.children = (ctx) => {
let elements = ()
let radii = resolve-radii(ctx, body)
let max-radius = calc.max(..radii.values())
let last = none
let i = 0
for element in body {
ctx = apply-style(ctx, element)
if "name" in element and "radius" in element {
let (row, col) = (
calc.quo(i, columns),
calc.rem(i, columns)
)
if calc.odd(row) {
element.coordinates = ((
(columns - col - 1) * (2*max-radius + spacing.x),
row * (2*max-radius + spacing.y)
),)
} else {
element.coordinates = ((
col * (2*max-radius + spacing.x),
row * (2*max-radius + spacing.y)
),)
}
i += 1
}
elements.push(element)
}
return elements
}
return (layout,)
}
/// Create a custom layout from a positioning function.
///
/// See "Creating custom layouts" for more information.
///
/// #example(breakable:true)[```
/// #let aut = range(6).fold((:), (d, s) => {d.insert("q"+str(s), none); d})
/// #finite.automaton(
/// aut,
/// initial: none, final: none,
/// layout:finite.layout.custom.with(positions:(..) => (
/// q0: (0,0), q1: (0,5), rest:(rel: (2,-1))
/// ))
/// )
/// ```]
///
/// - position (coordinate): Position of the anchor point.
/// - positions (function): A function #lambda("dictionary","dictionary","array",ret:"dictionary") to compute coordinates for each state.\
/// The function gets the current CETZ context, a dictionary of computed radii for each
/// state and a list with all state elements to position. The returned dictionary
/// contains each states name as a key and the new coordinate as a value.
///
/// The result may specify a `rest` key that is used as a default coordinate. This makes
/// sense in combination with a relative coordinate like `(rel:(2,0))`.
/// - name (string): Name for the element to access later.
/// - anchor (string): Name of the anchor to use for the layout.
/// - body (array): Array of CETZ elements to draw.
#let custom(
position, name: none, anchor: "left",
positions: (..) => (:),
body
) = {
if is.n(name) {
name = "layout" + body.map((e) => e.at("name", default:"")).join("-")
}
let l = base(position, name, anchor, body)
l.children = (ctx) => {
let radii = resolve-radii(ctx, body)
let states = body.filter((e) => "name" in e and "radius" in e)
let positions = positions(ctx, radii, states)
let default = positions.at("rest", default:(rel:(1,0)))
let elements = ()
for element in body {
ctx = apply-style(ctx, element)
if "name" in element {
element.coordinates = (positions.at(element.name, default:default),)
}
elements.push(element)
}
return elements
}
return (l,)
}
#let group(
position, name: none, anchor: "left",
grouping: 5,
spacing: .8,
layout: linear.with(dir:bottom),
body
) = {
if is.n(name) {
name = "layout" + body.map((e) => e.at("name", default:"")).join("-")
}
let base = base(position, name, anchor, body)
base.children = (ctx) => {
let groups = ()
let rest = ()
if is.int(grouping) {
for (i, element) in body.enumerate() {
if calc.rem(i, grouping) == 0 {
groups.push(())
}
groups.last().push(element)
}
} else if is.arr(grouping) {
// Collect States into groups
for (group) in grouping {
groups.push(())
for element in body {
if "name" in element and element.name in group {
groups.last().push(element)
}
}
}
for element in body {
if "name" not in element or not grouping.any((g) => element.name in g) {
rest.push(element)
}
}
}
let elements = ()
let last-name = none
for (i, group) in groups.enumerate() {
let group-layout
if is.arr(layout) {
if layout.len() > i {
group-layout = layout.at(i)
} else {
group-layout = layout.at(-1)
}
} else {
group-layout = layout
}
if is.n(last-name) {
elements += group-layout(position, anchor: "left", group)
} else {
elements += group-layout((rel:(spacing,0), to:last-name+".right"), anchor: "left", group)
}
last-name = elements.last().name
}
return elements + rest
}
return (base,)
}