-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.typ
596 lines (557 loc) · 17.3 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
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/* Config Setup */
/// #internal[Mostly internal.]
/// The counter used for the note icons.
///
/// If you use @note-numbering without @note-numbering.repeat, it is reccommended you reset this occasionally, e.g. per heading or per page.
/// #example(scale-preview: 100%, ```typc notecounter.update(1)```)
/// -> counter
#let notecounter = counter("notecounter")
/// Icons to use for note markers.
///
/// ```typc ("◆", "●", "■", "▲", "♥", "◇", "○", "□", "△", "♡")```
#let note-markers = ("◆", "●", "■", "▲", "♥", "◇", "○", "□", "△", "♡")
/// Icons to use for note markers, alternating filled/outlined.
///
/// ```typc ("●", "○", "◆", "◇", "■", "□", "▲", "△", "♥", "♡")```
#let note-markers-alternating = ("●", "○", "◆", "◇", "■", "□", "▲", "△", "♥", "♡")
/// Format note marker
/// -> content
#let note-numbering(
/// #example(```typ
/// #for i in array.range(1,15) [
/// #note-numbering(markers: note-markers, i)
/// ]\
/// #for i in array.range(1,15) [
/// #note-numbering(markers: note-markers-alternating, i)
/// ]
/// ```)
/// -> array(string)
markers: note-markers-alternating,
/// Whether to (```typc true```) loop over the icons, or (```typc false```) continue with numbers after icons run out.
/// #example(```typ
/// #for i in array.range(1,15) [
/// #note-numbering(repeat: true, i)
/// ]\
/// #for i in array.range(1,15) [
/// #note-numbering(repeat: false, i)
/// ]
/// ```)
/// -> boolean
repeat: true,
..,
/// -> int
number
) = {
let index = if repeat { calc.rem(number - 1, markers.len()) } else { number - 1 }
let symbol = if index < markers.len() { markers.at(index) } else { str(index + 1 - markers.len()) }
return text(weight: 900, font: "Inter", size: 5pt, style: "normal", fill: rgb(54%, 72%, 95%), symbol)
}
///#internal()
#let _fill_config(..config) = {
let config = config.named()
// default margins a4 are 2.5 cm
let inner = config.at("inner", default: (far: 5mm, width: 15mm, sep: 5mm))
let outer = config.at("outer", default: (far: 5mm, width: 15mm, sep: 5mm))
return (
inner: (
far: inner.at("far", default: 5mm),
width: inner.at("width", default: 15mm),
sep: inner.at("sep", default: 5mm),
),
outer: (
far: outer.at("far", default: 5mm),
width: outer.at("width", default: 15mm),
sep: outer.at("sep", default: 5mm),
),
top: config.at("top", default: 2.5cm),
bottom: config.at("bottom", default: 2.5cm),
book: config.at("book", default: false),
clearance: config.at("clearance", default: 12pt),
flush-numbers: config.at("flush-numbers", default: false),
numbering: config.at("numbering", default: note-numbering),
)
}
#let _config = state("_config", _fill_config())
/// This will update the marginalia config with the provided config options.
///
/// The default values for the margins have been chosen such that they match the default typst margins for a4. It is strongly recommended to change at least one of either `inner` or `outer` to be wide enough to actually contain text.
#let configure(
/// Inside/left margins.
/// - `far`: Distance between edge of page and margin (note) column.
/// - `width`: Width of the margin column.
/// - `sep`: Distance between margin column and main text column.
///
/// The page inside/left margin should equal `far` + `width` + `sep`.
///
/// If partial dictionary is given, it will be filled up with defaults.
/// -> dictionary
inner: (far: 5mm, width: 15mm, sep: 5mm),
/// Outside/right margins. Analogous to `inner`.
/// -> dictionary
outer: (far: 5mm, width: 15mm, sep: 5mm),
/// Top margin.
/// -> length
top: 2.5cm,
/// Bottom margin.
/// -> length
bottom: 2.5cm,
/// If ```typc true```, will use inside/outside margins, alternating on each page. If ```typc false```, will use left/right margins with all pages the same.
/// -> boolean
book: false,
/// Minimal vertical distance between notes and to wide blocks.
/// -> length
clearance: 8pt,
/// Disallow note icons hanging into the whitespace.
/// -> boolean
flush-numbers: false,
/// Function or `numbering`-string to generate the note markers from the `notecounter`.
/// -> function | string
numbering: note-numbering,
) = { }
#let configure(..config) = (
context {
_config.update(old => {
if type(old) != dictionary {
panic("marginalia _config should always be a dictionary")
}
_fill_config(..old, ..config)
})
}
)
/// Page setup helper
///
/// This will generate a dictionary ```typc ( margin: .. )``` compatible with the passed config.
/// This can then be spread into the page setup like so:
///```typ
/// #set page( ..page-setup(..config) )```
///
/// Takes the same options as @configure.
/// -> dictionary
#let page-setup(
/// Missing entries are filled with package defaults. Note: missing entries are _not_ taken from the current marginalia config, as this would require context.
/// -> dictionary
..config
) = {
let config = _fill_config(..config)
if config.book {
return (
margin: (
inside: config.inner.far + config.inner.width + config.inner.sep,
outside: config.outer.far + config.outer.width + config.outer.sep,
top: config.top,
bottom: config.bottom,
),
)
} else {
return (
margin: (
left: config.inner.far + config.inner.width + config.inner.sep,
right: config.outer.far + config.outer.width + config.outer.sep,
top: config.top,
bottom: config.bottom,
),
)
}
}
/// #internal[Mostly internal.]
/// Returns a dictionary with the keys `far`, `width`, `sep` containing the respective widths of the
/// left margin on the current page. (On both even and odd pages.)
///
/// Requires context.
/// -> dictionary
#let get-left() = {
let config = _config.get()
if not (config.book) or calc.odd(here().page()) {
return config.inner
} else {
return config.outer
}
}
/// #internal[Mostly internal.]
/// Returns a dictionary with the keys `far`, `width`, `sep` containing the respective widths of the
/// right margin on the current page. (On both even and odd pages.)
///
/// Requires context.
/// -> dictionary
#let get-right() = {
let config = _config.get()
if not (config.book) or calc.odd(here().page()) {
return config.outer
} else {
return config.inner
}
}
#let _note_extends_left = state("_note_extends_left", ("1": ()))
#let _note_offset_left(page_num) = {
let clearance = _config.get().clearance
let extends = _note_extends_left.final().at(page_num, default: ())
let offsets_down = ()
let cur = _config.get().top
for note in extends {
// 8pt spacing between nodes
if cur <= note.natural {
offsets_down.push(0pt)
cur = note.natural + note.height + clearance
} else if note.fix {
offsets_down.push(0pt)
cur = note.natural + note.height + clearance
} else {
offsets_down.push(cur - note.natural)
cur = cur + note.height + clearance
}
}
let offsets_final_rev = ()
let max = page.height - _config.get().bottom
for (index, note) in extends.enumerate().rev() {
if max >= note.natural + offsets_down.at(index) + note.height {
offsets_final_rev.push(offsets_down.at(index))
max = note.natural + offsets_down.at(index) - clearance
} else if note.fix {
offsets_final_rev.push(0pt)
max = calc.min(note.natural - clearance, max)
} else {
offsets_final_rev.push(max - note.natural - note.height)
max = max - note.height - clearance
}
}
offsets_final_rev.rev()
}
#let _note_extends_right = state("_note_extends_right", ("1": ()))
#let _note_offset_right(page_num) = {
let clearance = _config.get().clearance
let extends = _note_extends_right.final().at(page_num, default: ())
let offsets_down = ()
let cur = _config.get().top
for note in extends {
// 8pt spacing between nodes
if cur <= note.natural {
offsets_down.push(0pt)
cur = note.natural + note.height + clearance
} else if note.fix {
offsets_down.push(0pt)
cur = note.natural + note.height + clearance
} else {
offsets_down.push(cur - note.natural)
cur = cur + note.height + clearance
}
}
let offsets_final_rev = ()
let max = page.height - _config.get().bottom
for (index, note) in extends.enumerate().rev() {
if max >= note.natural + offsets_down.at(index) + note.height {
offsets_final_rev.push(offsets_down.at(index))
max = note.natural + offsets_down.at(index) - clearance
} else if note.fix {
offsets_final_rev.push(0pt)
max = calc.min(note.natural - clearance, max)
} else {
offsets_final_rev.push(max - note.natural - note.height)
max = max - note.height - clearance
}
}
offsets_final_rev.rev()
}
// absolute left
/// #internal()
#let _note_left(dy: 0pt, body) = (
context {
let dy = dy.to-absolute()
let anchor = here().position()
let pagewidth = page.width
let page = here().page()
let width = get-left().width
let notebox = box(width: width, body)
let height = measure(notebox).height
let lineheight = measure([X]).height
lineheight = calc.min(lineheight, height)
let natural_position = anchor.y + dy - lineheight
let current = _note_extends_left.get().at(str(page), default: ())
let index = current.len()
_note_extends_left.update(old => {
let oldpage = old.at(str(page), default: ())
oldpage.push((natural: natural_position, height: height, fix: false))
old.insert(str(page), oldpage)
old
})
let vadjust = dy - lineheight + _note_offset_left(str(page)).at(index, default: 0pt)
let hadjust = get-left().far - anchor.x
box(
place(
dx: hadjust,
dy: vadjust,
notebox,
),
)
}
)
// absolute right
/// #internal()
#let _note_right(dy: 0pt, body) = (
context {
let dy = dy.to-absolute()
let anchor = here().position()
let pagewidth = page.width
let page = here().page()
let width = get-right().width
let notebox = box(width: width, body)
let height = measure(notebox).height
let lineheight = measure([X]).height
lineheight = calc.min(lineheight, height)
let natural_position = anchor.y + dy - lineheight
let current = _note_extends_right.get().at(str(page), default: ())
let index = current.len()
_note_extends_right.update(old => {
let oldpage = old.at(str(page), default: ())
oldpage.push((natural: natural_position, height: height, fix: false))
old.insert(str(page), oldpage)
old
})
let vadjust = dy - lineheight + _note_offset_right(str(page)).at(index, default: 0pt)
let hadjust = pagewidth - anchor.x - get-right().far - get-right().width
box(
width: 0pt,
place(
dx: hadjust,
dy: vadjust,
notebox,
),
)
}
)
/// Create a marginnote.
/// Will adjust it's position downwards to avoid previously placed notes, and upwards to avoid extending past the bottom margin.
#let note(
/// Whether to put a mark.
/// -> boolean
numbered: true,
/// Whether to put it in the opposite (inner/left) margin.
/// -> boolean
reverse: false,
/// Inital vertical offset of the note.
/// Note may get shifted still to avoid other notes.
/// -> length
dy: 0pt,
/// -> content
body
) = {
set text(size: 7.5pt, style: "normal", weight: "regular")
if numbered {
notecounter.step()
let body = context if _config.get().flush-numbers {
set par(spacing: 0.8em, leading: 0.4em, hanging-indent: 0pt)
notecounter.display(_config.get().numbering)
h(1.5pt)
body
} else {
set par(spacing: 0.8em, leading: 0.4em, hanging-indent: 0pt)
box(
width: 0pt,
{
h(-8pt)
h(1fr)
notecounter.display(_config.get().numbering)
h(1fr)
},
)
h(0pt, weak: true)
body
}
h(0pt, weak: true)
box(context {
h(1.5pt, weak: true)
notecounter.display(_config.get().numbering)
if _config.get().book and calc.even(here().page()) {
if reverse {
_note_right(dy: dy, body)
} else {
_note_left(dy: dy, body)
}
} else {
if reverse {
_note_left(dy: dy, body)
} else {
_note_right(dy: dy, body)
}
}
})
} else {
h(0pt, weak: true)
let body = {
set par(spacing: 0.8em, leading: 0.4em, hanging-indent: 0pt)
body
}
box(context {
if _config.get().book and calc.even(here().page()) {
if reverse {
_note_right(dy: dy, body)
} else {
_note_left(dy: dy, body)
}
} else {
if reverse {
_note_left(dy: dy, body)
} else {
_note_right(dy: dy, body)
}
}
})
}
}
/// Creates a figure in the margin.
/// -> content
#let notefigure(
/// The figure content, e.g.~an image. Pass-through to ```typ #figure()```, but used to adjust the vertical position.
/// -> content
content,
/// Put the notefigure in the opposite margin.
/// -> boolean
reverse: false,
/// How much to shift the note. ```typc 100%``` corresponds to the height of `content` + `gap`.
/// -> relative length
dy: 0pt - 100%,
/// Whether to put a mark.
/// -> boolean
numbered: false,
/// Pass-through to ```typ #figure()```, but used to adjust the vertical position.
/// -> length
gap: 0.55em,
/// A label to attach to the figure.
/// -> none | label
label: none,
/// Pass-through to ```typ #figure()```.
/// -> arguments
..figureargs,
) = (
context {
set figure.caption(position: bottom)
show figure.caption: it => {
set align(left)
if numbered {
context if _config.get().flush-numbers {
notecounter.display(_config.get().numbering)
h(1.5pt)
} else {
box(
width: 0pt,
{
h(-8pt)
h(1fr)
notecounter.display(_config.get().numbering)
h(1fr)
},
)
h(0pt, weak: true)
}
}
it
}
let width = if reverse or (_config.get().book and calc.even(here().page())) {
get-left().width
} else {
get-right().width
}
let height = measure(width: width, content).height + measure(text(size: 7.5pt, v(gap))).height
if numbered {
h(1.5pt, weak: true)
notecounter.display(_config.get().numbering)
} else {
h(0pt, weak: true)
}
let dy = 0% + 0pt + dy
note(
reverse: reverse,
dy: dy.length + dy.ratio * height,
numbered: false,
)[
#figure(
content,
gap: gap,
placement: none,
..figureargs,
) #label
]
}
)
/// Creates a block that extends into the outside/right margin.
///
/// Note: This does not handle page-breaks sensibly.
/// If ```typc config.book = false```, this is not a problem, as then the margins on all pages are the same.
/// However, when using alternating page margins, a multi-page `wideblock` will not work properly.
/// To be able to set this appendix in a many-page wideblock, this code was used:
///```typst
///#configure(..config, book: false)
///#set page(..page-setup(..config, book: false))
///#wideblock(reverse: true)[...]
///```
/// -> content
#let wideblock(
/// Whether to extend into the inside/left margin instead.
/// -> boolean
reverse: false,
/// Whether to extend into both margins. Cannot be combined with `reverse`.
/// -> boolean
double: false,
/// -> content
body
) = (
context {
if double and reverse {
panic("Cannot be both reverse and double wide.")
}
let oddpage = calc.odd(here().page())
let config = _config.get()
let left = if not (config.book) or oddpage {
if double or reverse {
config.inner.width + config.inner.sep
} else {
0pt
}
} else {
if reverse {
0pt
} else {
config.outer.width + config.outer.sep
}
}
let right = if not (config.book) or oddpage {
if reverse {
0pt
} else {
config.outer.width + config.outer.sep
}
} else {
if double or reverse {
config.inner.width + config.inner.sep
} else {
0pt
}
}
let position = here().position().y
let page_num = str(here().page())
let left-margin = get-left()
let right-margin = get-right()
let linewidth = page.width - left-margin.far - left-margin.width - left-margin.sep - right-margin.far - right-margin.width - right-margin.sep
let height = measure(width: linewidth + left + right, body).height
if left != 0pt {
let current = _note_extends_left.get().at(page_num, default: ())
let index = current.len()
_note_extends_left.update(old => {
let oldpage = old.at(page_num, default: ())
oldpage.push((natural: position, height: height, fix: true))
old.insert(page_num, oldpage)
old
})
}
if right != 0pt {
let current = _note_extends_right.get().at(page_num, default: ())
let index = current.len()
_note_extends_right.update(old => {
let oldpage = old.at(page_num, default: ())
oldpage.push((natural: position, height: height, fix: true))
old.insert(page_num, oldpage)
old
})
}
pad(left: -left, right: -right, body)
}
)