forked from jneug/typst-finite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.typ
395 lines (338 loc) · 9.6 KB
/
util.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
#import "@preview/t4t:0.3.2": get, def, is, assert, math
#import "@preview/cetz:0.1.1": vector, matrix, draw
#import draw: util, styles, cmd, coordinate
#import util.bezier: cubic-point, cubic-derivative, cubic-through-3points
// =================================
// Defaults
// =================================
#let default-style = (
state: (
fill: auto,
stroke: auto,
radius: .6,
label: (
text: auto,
size: auto
)
),
transition: (
curve: .75,
stroke: auto,
label: (
text: "",
size: 1em,
color: auto,
pos: .5,
dist: .33,
angle: auto
)
)
)
// =================================
// Vectors
// =================================
/// Set the length of a vector.
#let vector-set-len(v, len) = if vector.len(v) == 0 {
return v
} else {
return vector.scale(vector.norm(v), len)
}
/// Compute a normal for a 2d vector. The normal will be pointing to the right
/// of the original vector.
#let vector-normal(v) = vector.norm((-v.at(1), v.at(0), 0))
/// Returns a vector for an alignment.
#let align-to-vec( a ) = {
let v = (
("none": 0, "left": -1, "right": 1).at(repr(get.x-align(a, default:none))),
("none": 0, "top": 1, "bottom": -1).at(repr(get.y-align(a, default:none)))
)
return vector.norm(v)
}
/// Rotates a vector by #arg[angle] degree around the origin.
#let vector-rotate(vec, angle) = {
let (x, y, ..) = vec
return (
calc.cos(angle) * x - calc.sin(angle) * y,
calc.sin(angle) * x + calc.cos(angle) * y
)
}
// =================================
// Bezier
// =================================
/// Compute a normal vector for a point on a cubic bezier curve.
#let cubic-normal(a, b, c, d, t) = {
let qd = cubic-derivative(a, b, c, d, t)
if vector.len(qd) == 0 {
return (0, 1, 0)
} else {
return vector-normal(qd)
}
}
/// Compute the mid point of a quadratic bezier curve.
#let mid-point(a, b, c, d) = cubic-point(a, b, c, d, .5)
// =================================
// Helpers
// =================================
/// Calculate the control point for a transition.
#let cubic-pts(a, b, curve:1) = {
if curve == 0 {
return (a, b, b, a)
}
let ab = vector.sub(b, a)
let X = vector.add(
vector.add(
a,
vector.scale(ab, .5)),
vector.scale(
vector-normal(ab),
curve))
return cubic-through-3points(a, X, b)
}
/// Calculate the direction vector for a transition mark (arrowhead)
#let mark-dir(a, b, c, d, scale:1) = vector-set-len(cubic-derivative(a, b, c, d, 1), scale)
/// Calculate the location for a transitions label, based
/// on its bezier points.
#let label-pt(a, b, c, d, style, loop:false) = {
let pos = style.label.at("pos", default: default-style.transition.label.pos)
let dist = style.label.at("dist", default: default-style.transition.label.dist)
let curve = style.at("curve", default: default-style.transition.curve)
let pt = cubic-point(a, b, c, d, pos)
let n = cubic-normal(a, b, c, d, pos)
if loop and curve < 0 {
dist *= -1
}
return vector.add(pt,
vector.scale(n, dist))
}
/// Calculate start, end and ctrl points for a transition loop.
///
/// - start (vector): Center of the state.
/// - start-radius (length): Radius of the state.
/// - curve (float): Curvature of the transition.
/// - anchor (alignment): Anchorpoint on the state
#let loop-pts(start, start-radius, anchor:top, curve:1) = {
anchor = vector-set-len(align-to-vec(anchor), start-radius)
let end = vector.add(
start,
vector-rotate(anchor, -22.5deg)
)
let start = vector.add(
start,
vector-rotate(anchor, 22.5deg)
)
if curve < 0 {
(start, end) = (end, start)
} else if curve == 0 {
curve = start-radius
}
let (start, end, c1, c2) = cubic-pts(start, end, curve:curve)
if curve < 0 {
(c1, c2) = (c2, c1)
}
let d = vector.scale(vector.sub(c2, c1), curve * 4)
c1 = vector.sub(c1, d)
c2 = vector.add(c2, d)
return (start, end, c1, c2)
}
/// Calculate start, end and ctrl points for a transition.
///
/// - start (vector): Center of the start state.
/// - end (vector): Center of the end state.
/// - start-radius (length): Radius of the start state.
/// - end-radius (length): Radius of the end state.
/// - curve (float): Curvature of the transition.
#let transition-pts(start, end, start-radius, end-radius, curve:1, anchor:top) = {
// Is it a loop?
if start == end {
return loop-pts(start, start-radius, curve:curve, anchor:anchor)
} else {
let (start, end, ctrl1, ctrl2) = cubic-pts(start, end, curve:curve)
start = vector.add(
start,
vector-set-len(
vector.sub(
ctrl1,
start),
start-radius))
end = vector.add(
end,
vector-set-len(
vector.sub(
end,
ctrl2),
-end-radius))
return (
start,
end,
ctrl1,
ctrl2
)
}
}
/// Fits (text) content inside the available space.
///
/// - ctx (dictionary): The canvas context.
/// - content (string, content): The content to fit.
/// - size (length,auto): The initial text size.
/// - min-size (length): The minimal text size to set.
#let fit-content( ctx, width, height, content, size:auto, min-size:6pt ) = {
let s = def.if-auto(ctx.length, size)
let m = (width: 2*width, height: 2*height)
while (m.height > height or m.width > height) and s > min-size {
s = s*.88
m = measure({set text(s); content}, ctx.typst-style)
}
s = calc.max(min-size, s)
{set text(s); content}
}
/// Prepares the CeTZ context for use with finite
#let prepare-ctx(ctx, force:false) = {
if force or "finite" not in ctx {
// supposed to store state information at some point
ctx.insert("finite", (states:()))
// add default state styles to context
if "state" not in ctx.style {
ctx.style.insert("state", default-style.state)
} else {
if "label" in ctx.style.state and not is.dict(ctx.style.state.label) {
ctx.style.state.label = (text: ctx.style.state.label)
}
ctx.style.state = styles.resolve(default-style.state, ctx.style.state)
}
// add default transition styles to context
if "transition" not in ctx.style {
ctx.style.insert("transition", default-style.transition)
} else {
if "label" in ctx.style.transition and not is.dict(ctx.style.transition.label) {
ctx.style.transition.label = (text: ctx.style.transition.label)
}
ctx.style.transition = styles.resolve(default-style.transition, ctx.style.transition)
}
}
return ctx
}
// Changes a transition table from the format (`state`: `inputs`) to (`input`: `states`) or vice versa.
#let transpose-table(table) = {
let ttable = (:)
for (key, values) in table {
let new-values = (:)
if is.not-none(values) {
for (kk, vv) in values {
for i in def.as-arr(vv) {
if is.not-none(i) {
i = str(i)
if i not in new-values {
new-values.insert(i, (kk,))
} else {
new-values.at(i).push(kk)
}
}
}
}
}
ttable.insert(key, new-values)
}
return ttable
}
/// Gets a list of all inputs from a transition table.
#let get-inputs(table, transpose:true) = {
if transpose {
table = transpose-table(table)
}
let inputs = ()
for (_, values) in table {
for (inp, _) in values {
if inp not in inputs {
inputs.push(str(inp))
}
}
}
return inputs.sorted()
}
/// Creates a full specification for a finite automaton.
#let to-spec(spec, states:auto, initial:auto, final:auto, inputs:auto) = {
if "transitions" not in spec {
spec = (transitions: spec)
}
if "states" not in spec {
if is.a(states) {
states = spec.transitions.keys()
}
spec.insert("states", states)
}
if "initial" not in spec {
if is.a(initial) {
initial = spec.states.first()
}
spec.insert("initial", initial)
}
if "final" not in spec {
if is.a(final) {
final = spec.states.last()
} else if is.n(final) {
final = ()
}
spec.insert("final", final)
}
if "inputs" not in spec {
if is.a(inputs) {
inputs = get-inputs(spec.transitions)
}
spec.insert("inputs", inputs)
} else {
spec.inputs = spec.inputs.map(str).sorted()
}
return spec
}
// Unused
#let calc-bounds( positions ) = {
let bounds = (
x: positions.first().at(0),
y: positions.first().at(1),
width: positions.first().at(0),
height: positions.first().at(1)
)
for (x,y) in positions {
bounds.x = calc.min(bounds.x, x)
bounds.y = calc.min(bounds.y, y)
bounds.width = calc.max(bounds.width, x)
bounds.height = calc.max(bounds.height, y)
}
bounds.width = bounds.width - bounds.x
bounds.height = bounds.height - bounds.y
return bounds
}
#let calc-shift(anchor, bounds:none) = {
let shift = (x: -.5, y: -.5)
if anchor.ends-with("right") {
shift.x -= .5
} else if anchor.ends-with("left") {
shift.x += .5
}
if anchor.starts-with("top") {
shift.y -= .5
} else if anchor.starts-with("bottom") {
shift.y += .5
}
if bounds != none {
shift.x *= bounds.width
shift.y *= bounds.height
}
return shift
}
#let shift-by-anchor(positions, anchor) = {
let bounds = calc-bounds(positions.values())
let shift = calc-shift(anchor, bounds:bounds)
for (name, pos) in positions {
let (x, y) = pos
positions.at(name) = (x + shift.x, y + shift.y)
}
return positions
}
#let resolve-radius(state, style) = if state in style and "radius" in style.at(state) {
return style.at(state).radius
} else if "state" in style and "radius" in style.state {
return style.state.radius
} else {
return default-style.state.radius
}