-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.typ
288 lines (279 loc) · 9.58 KB
/
lib.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
#import "simple-arrow.typ": simple-arrow, double-arrow
#import "pinit-fletcher.typ": pinit-fletcher-edge
#import "pinit-core.typ": *
// -----------------------------------------------
// Libs
// -----------------------------------------------
/// Draw a rectangular shape on the page **containing all pins** with optional extended width and height.
///
/// - `dx`: [`length`] — Offset X relative to the min-left of pins.
/// - `dy`: [`length`] — Offset Y relative to the min-top of pins.
/// - `extended-width`: [`length`] — Optional extended width of the rectangular shape.
/// - `extended-height`: [`length`] — Optional extended height of the rectangular shape.
/// - `pin1`: [`pin`] — One of these pins.
/// - `pin2`: [`pin`] — One of these pins.
/// - `pin3`: [`pin`] — One of these pins, optionally.
/// - `...args`: Additional named arguments or settings for [`rect`](https://typst.app/docs/reference/visualize/rect/), like `fill`, `stroke` and `radius`.
#let pinit-rect(
dx: 0em,
dy: -1em,
extended-width: 0em,
extended-height: 1.4em,
..args,
) = {
pinit(
..args.pos(),
callback: (..positions) => {
positions = positions.pos()
let min-x = calc.min(..positions.map(loc => loc.x))
let max-x = calc.max(..positions.map(loc => loc.x))
let min-y = calc.min(..positions.map(loc => loc.y))
let max-y = calc.max(..positions.map(loc => loc.y))
absolute-place(
dx: min-x + dx,
dy: min-y + dy,
rect(
width: max-x - min-x + extended-width,
height: max-y - min-y + extended-height,
..args.named(),
),
)
},
)
}
/// Highlight a specific area on the page with a filled color and optional radius and stroke. It is just a simply styled `pinit-rect`.
///
// - `fill`: [`color`] — The fill color for the highlighted area.
// - `radius`: [`length`] — Optional radius for the highlight.
// - `stroke`: [`stroke`] — Optional stroke width for the highlight.
// - `dx`: [`length`] — Offset X relative to the min-left of pins.
// - `dy`: [`length`] — Offset Y relative to the min-top of pins.
// - `extended-width`: [`length`] — Optional extended width of the rectangular shape.
// - `extended-height`: [`length`] — Optional extended height of the rectangular shape.
// - `pin1`: [`pin`] — One of these pins.
// - `pin2`: [`pin`] — One of these pins.
// - `pin3`: [`pin`] — One of these pins, optionally.
// - `...args`: Additional arguments or settings for [`pinit-rect`](#pinit-rect).
#let pinit-highlight(
fill: rgb(255, 0, 0, 20),
radius: 5pt,
stroke: 0pt,
dx: 0em,
dy: -1em,
extended-width: 0em,
extended-height: 1.4em,
..args,
) = {
pinit-rect(
fill: fill,
radius: radius,
stroke: stroke,
dx: dx,
dy: dy,
extended-width: extended-width,
extended-height: extended-height,
..args,
)
}
/// Draw a line on the page between two specified pins with an optional stroke.
///
/// - `stroke`: [`stroke`] — The stroke for the line.
/// - `start-dx`: [`length`] — Offset X relative to the start pin.
/// - `start-dy`: [`length`] — Offset Y relative to the start pin.
/// - `end-dx`: [`length`] — Offset X relative to the end pin.
/// - `end-dy`: [`length`] — Offset Y relative to the end pin.
/// - `start`: [`pin`] — The start pin.
/// - `end`: [`pin`] — The end pin.
#let pinit-line(
stroke: 1pt,
start-dx: 0pt,
start-dy: 0pt,
end-dx: 0pt,
end-dy: 0pt,
start,
end,
) = {
pinit(
start,
end,
callback: (start-pos, end-pos) => {
absolute-place(
line(
stroke: stroke,
start: (
start-pos.x + start-dx,
start-pos.y + start-dy,
),
end: (
end-pos.x + end-dx,
end-pos.y + end-dy,
),
),
)
},
)
}
/// Draw an line from a specified pin to a point on the page with optional settings.
///
/// - `stroke`: [`stroke`] — The stroke for the line.
/// - `pin-dx`: [`length`] — Offset X of arrow start relative to the pin.
/// - `pin-dy`: [`length`] — Offset Y of arrow start relative to the pin.
/// - `body-dx`: [`length`] — Offset X of arrow end relative to the body.
/// - `body-dy`: [`length`] — Offset Y of arrow end relative to the body.
/// - `offset-dx`: [`length`] — Offset X relative to the pin.
/// - `offset-dy`: [`length`] — Offset Y relative to the pin.
/// - `pin-name`: [`pin`] — The name of the pin to start from.
/// - `body`: [`content`] — The content to draw the arrow to.
#let pinit-line-to(
pin-dx: 5pt,
pin-dy: 5pt,
body-dx: 5pt,
body-dy: 5pt,
offset-dx: 35pt,
offset-dy: 35pt,
pin-name,
body,
..args,
) = {
pinit-line(pin-name, pin-name, start-dx: pin-dx, start-dy: pin-dy, end-dx: offset-dx, end-dy: offset-dy, ..args)
pinit-place(pin-name, body, dx: offset-dx + body-dx, dy: offset-dy + body-dy)
}
/// Draw an arrow between two specified pins with optional settings.
///
/// - `start-dx`: [`length`] — Offset X relative to the start pin.
/// - `start-dy`: [`length`] — Offset Y relative to the start pin.
/// - `end-dx`: [`length`] — Offset X relative to the end pin.
/// - `end-dy`: [`length`] — Offset Y relative to the end pin.
/// - `start`: [`pin`] — The start pin.
/// - `end`: [`pin`] — The end pin.
/// - `...args`: Additional arguments or settings for [`simple-arrow`](#simple-arrow), like `fill`, `stroke` and `thickness`.
#let pinit-arrow(
start-dx: 0pt,
start-dy: 0pt,
end-dx: 0pt,
end-dy: 0pt,
start,
end,
..args,
) = {
pinit(
start,
end,
callback: (start-pos, end-pos) => {
absolute-place(
simple-arrow(
start: (
start-pos.x + start-dx,
start-pos.y + start-dy,
),
end: (
end-pos.x + end-dx,
end-pos.y + end-dy,
),
..args,
),
)
},
)
}
/// Draw an double arrow between two specified pins with optional settings.
///
/// - `start-dx`: [`length`] — Offset X relative to the start pin.
/// - `start-dy`: [`length`] — Offset Y relative to the start pin.
/// - `end-dx`: [`length`] — Offset X relative to the end pin.
/// - `end-dy`: [`length`] — Offset Y relative to the end pin.
/// - `start`: [`pin`] — The start pin.
/// - `end`: [`pin`] — The end pin.
/// - `...args`: Additional arguments or settings for [`double-arrow`](#double-arrow), like `fill`, `stroke` and `thickness`.
#let pinit-double-arrow(
start-dx: 0pt,
start-dy: 0pt,
end-dx: 0pt,
end-dy: 0pt,
start,
end,
..args,
) = {
pinit(
start,
end,
callback: (start-pos, end-pos) => {
absolute-place(
double-arrow(
start: (
start-pos.x + start-dx,
start-pos.y + start-dy,
),
end: (
end-pos.x + end-dx,
end-pos.y + end-dy,
),
..args,
),
)
},
)
}
/// Draw an arrow from a specified pin to a point on the page with optional settings.
/// - `pin-dx`: [`length`] — Offset X of arrow start relative to the pin.
/// - `pin-dy`: [`length`] — Offset Y of arrow start relative to the pin.
/// - `body-dx`: [`length`] — Offset X of arrow end relative to the body.
/// - `body-dy`: [`length`] — Offset Y of arrow end relative to the body.
/// - `offset-dx`: [`length`] — Offset X relative to the pin.
/// - `offset-dy`: [`length`] — Offset Y relative to the pin.
/// - `double`: [`bool`] — Draw a double arrow, default is `false`.
/// - `pin-name`: [`pin`] — The name of the pin to start from.
/// - `body`: [`content`] — The content to draw the arrow to.
/// - `...args`: Additional arguments or settings for [`simple-arrow`](#simple-arrow), like `fill`, `stroke` and `thickness`.
#let pinit-point-to(
pin-dx: 5pt,
pin-dy: 5pt,
body-dx: 5pt,
body-dy: 5pt,
offset-dx: 35pt,
offset-dy: 35pt,
double: false,
pin-name,
body,
..args,
) = {
let arrow-fn = if double {
pinit-double-arrow
} else {
pinit-arrow
}
arrow-fn(pin-name, pin-name, start-dx: pin-dx, start-dy: pin-dy, end-dx: offset-dx, end-dy: offset-dy, ..args)
pinit-place(pin-name, body, dx: offset-dx + body-dx, dy: offset-dy + body-dy)
}
/// Draw an arrow from a point on the page to a specified pin with optional settings.
///
/// - `pin-dx`: [`length`] — Offset X relative to the pin.
/// - `pin-dy`: [`length`] — Offset Y relative to the pin.
/// - `body-dx`: [`length`] — Offset X relative to the body.
/// - `body-dy`: [`length`] — Offset Y relative to the body.
/// - `offset-dx`: [`length`] — Offset X relative to the left edge of the page.
/// - `offset-dy`: [`length`] — Offset Y relative to the top edge of the page.
/// - `double`: [`bool`] — Draw a double arrow, default is `false`.
/// - `pin-name`: [`pin`] — The name of the pin that the arrow to.
/// - `body`: [`content`] — The content to draw the arrow from.
/// - `...args`: Additional arguments or settings for [`simple-arrow`](#simple-arrow), like `fill`, `stroke` and `thickness`.
#let pinit-point-from(
pin-dx: 5pt,
pin-dy: 5pt,
body-dx: 5pt,
body-dy: 5pt,
offset-dx: 35pt,
offset-dy: 35pt,
double: false,
pin-name,
body,
..args,
) = {
let arrow-fn = if double {
pinit-double-arrow
} else {
pinit-arrow
}
arrow-fn(pin-name, pin-name, start-dx: offset-dx, start-dy: offset-dy, end-dx: pin-dx, end-dy: pin-dy, ..args)
pinit-place(pin-name, body, dx: offset-dx + body-dx, dy: offset-dy + body-dy)
}