-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlib.typ
606 lines (526 loc) · 16.6 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
597
598
599
600
601
602
603
604
605
606
// ####################
// # typst-letter-pro #
// ####################
//
// Project page:
// https://github.com/Sematre/typst-letter-pro
//
// References:
// https://de.wikipedia.org/wiki/DIN_5008
// https://www.deutschepost.de/de/b/briefvorlagen/normbrief-din-5008-vorlage.html
// https://www.deutschepost.de/content/dam/dpag/images/P_p/printmailing/downloads/automationsfaehige-briefsendungen-2023.pdf
// https://www.edv-lehrgang.de/din-5008/
// https://www.edv-lehrgang.de/anschriftfeld-im-din-5008-geschaeftsbrief/
// ##################
// # Letter formats #
// ##################
#let letter-formats = (
"DIN-5008-A": (
folding-mark-1-pos: 87mm,
folding-mark-2-pos: 87mm + 105mm,
header-size: 27mm,
),
"DIN-5008-B": (
folding-mark-1-pos: 105mm,
folding-mark-2-pos: 105mm + 105mm,
header-size: 45mm,
),
)
// ##################
// # Generic letter #
// ##################
/// This function takes your whole document as its `body` and formats it as a simple letter.
///
/// - format (string): The format of the letter, which decides the position of the folding marks and the size of the header.
/// #table(
/// columns: (1fr, 1fr, 1fr),
/// stroke: 0.5pt + gray,
///
/// text(weight: "semibold")[Format],
/// text(weight: "semibold")[Folding marks],
/// text(weight: "semibold")[Header size],
///
/// [DIN-5008-A], [87mm, 192mm], [27mm],
/// [DIN-5008-B], [105mm, 210mm], [45mm],
/// )
///
/// - header (content, none): The header that will be displayed at the top of the first page.
/// - footer (content, none): The footer that will be displayed at the bottom of the first page. It automatically grows upwords depending on its body. Make sure to leave enough space in the page margins.
///
/// - folding-marks (boolean): The folding marks that will be displayed at the left margin.
/// - hole-mark (boolean): The hole mark that will be displayed at the left margin.
///
/// - address-box (content, none): The address box that will be displayed below the header on the left.
///
/// - information-box (content, none): The information box that will be displayed below below the header on the right.
/// - reference-signs (array, none): The reference signs that will be displayed below below the the address box. The array has to be a collection of tuples with 2 content elements.
///
/// Example:
/// ```typ
/// (
/// ([Foo], [bar]),
/// ([Hello], [World]),
/// )
/// ```
///
/// - page-numbering (auto, string, function, none): Defines the format of the page numbers.
/// #table(
/// columns: (auto, 1fr),
/// stroke: 0.5pt + gray,
///
/// text(weight: "semibold")[Type], text(weight: "semibold")[Description],
/// [auto], [Automatically determines the document language and chooses an appropriate translation.],
/// [string], [A numbering pattern as specified by the official documentation of the #link("https://typst.app/docs/reference/model/numbering/", text(blue)[_numbering_]) function.],
/// [function], [
/// A function that returns the page number for each page.\
/// Parameters:
/// - current-page (integer)
/// - page-count (integer)
/// Return type: _content_
/// ],
/// [none], [Disable page numbering.],
/// )
///
/// - margin (dictionary): The margin of the letter.
///
/// The dictionary can contain the following fields: _left_, _right_, _top_, _bottom_.\
/// Missing fields will be set to the default.
/// Note: There is no _rest_ field.
///
/// - body (content, none): The content of the letter
/// -> content
#let letter-generic(
format: "DIN-5008-B",
header: none,
footer: none,
folding-marks: true,
hole-mark: true,
address-box: none,
information-box: none,
reference-signs: none,
page-numbering: auto,
margin: (
left: 25mm,
right: 20mm,
top: 20mm,
bottom: 20mm,
),
body,
) = {
if not letter-formats.keys().contains(format) {
panic("Invalid letter format! Options: " + letter-formats.keys().join(", "))
}
margin = (
left: margin.at("left", default: 25mm),
right: margin.at("right", default: 20mm),
top: margin.at("top", default: 20mm),
bottom: margin.at("bottom", default: 20mm),
)
set page(
paper: "a4",
flipped: false,
margin: margin,
background: {
if folding-marks {
// folding mark 1
place(top + left, dx: 5mm, dy: letter-formats.at(format).folding-mark-1-pos, line(
length: 2.5mm,
stroke: 0.25pt + black
))
// folding mark 2
place(top + left, dx: 5mm, dy: letter-formats.at(format).folding-mark-2-pos, line(
length: 2.5mm,
stroke: 0.25pt + black
))
}
if hole-mark {
// hole mark
place(left + top, dx: 5mm, dy: 148.5mm, line(
length: 4mm,
stroke: 0.25pt + black
))
}
},
footer-descent: 0%,
footer: context {
show: pad.with(top: 12pt, bottom: 12pt)
let current-page = here().page()
let page-count = counter(page).final().first()
grid(
columns: 1fr,
rows: (0.65em, 1fr),
row-gutter: 12pt,
if page-count > 1 {
if page-numbering == auto {
if text.lang == "de" {
align(right)[Seite #current-page von #page-count]
} else {
align(right)[Page #current-page of #page-count]
}
} else if type(page-numbering) == str {
align(right, numbering(page-numbering, current-page, page-count))
} else if type(page-numbering) == function {
align(right, page-numbering(current-page, page-count))
} else if page-numbering != none {
panic("Unsupported option type!")
}
},
if current-page == 1 {
footer
}
)
},
)
// Reverse the margin for the header, the address box and the information box
pad(top: -margin.top, left: -margin.left, right: -margin.right, {
grid(
columns: 100%,
rows: (letter-formats.at(format).header-size, 45mm),
// Header box
header,
// Address / Information box
pad(left: 20mm, right: 10mm, {
grid(
columns: (85mm, 75mm),
rows: 45mm,
column-gutter: 20mm,
// Address box
address-box,
// Information box
pad(top: 5mm, information-box)
)
}),
)
})
v(12pt)
// Reference signs
if (reference-signs != none) and (reference-signs.len() > 0) {
grid(
// Total width: 175mm
// Delimiter: 4.23mm
// Cell width: 50mm - 4.23mm = 45.77mm
columns: (45.77mm, 45.77mm, 45.77mm, 25mm),
rows: 12pt * 2,
gutter: 12pt,
..reference-signs.map(sign => {
let (key, value) = sign
text(size: 8pt, key)
linebreak()
text(size: 10pt, value)
})
)
}
// Add body.
body
}
// ####################
// # Helper functions #
// ####################
/// Creates a simple header with a name, an address and extra information.
///
/// - name (content, none): Name of the sender
/// - address (content, none): Address of the sender
/// - extra (content, none): Extra information about the sender
#let header-simple(name, address, extra: none) = {
set text(size: 10pt)
if name != none {
strong(name)
linebreak()
}
if address != none {
address
linebreak()
}
if extra != none {
extra
}
}
/// Creates a simple sender box with a name and an address.
///
/// - name (content, none): Name of the sender
/// - address (content, none): Address of the sender
#let sender-box(name: none, address) = rect(width: 85mm, height: 5mm, stroke: none, inset: 0pt, {
set text(size: 7pt)
set align(horizon)
pad(left: 5mm, underline(offset: 2pt, {
if name != none {
name
}
if (name != none) and (address != none) {
", "
}
if address != none {
address
}
}))
})
/// Creates a simple annotations box.
///
/// - content (content, none): The content
#let annotations-box(content) = {
set text(size: 7pt)
set align(bottom)
pad(left: 5mm, bottom: 2mm, content)
}
/// Creates a simple recipient box.
///
/// - content (content, none): The content
#let recipient-box(content) = {
set text(size: 10pt)
set align(top)
pad(left: 5mm, content)
}
/// Creates a simple address box with 2 fields.
///
/// The width is is determined automatically. Row heights:
/// #table(
/// columns: 3cm,
/// rows: (17.7mm, 27.3mm),
/// stroke: 0.5pt + gray,
/// align: center + horizon,
///
/// [sender\ 17.7mm],
/// [recipient\ 27.3mm],
/// )
///
/// See also: _address-tribox_
///
/// - sender (content, none): The sender box
/// - recipient (content, none): The recipient box
#let address-duobox(sender, recipient) = {
grid(
columns: 1,
rows: (17.7mm, 27.3mm),
sender,
recipient,
)
}
/// Creates a simple address box with 3 fields and optional repartitioning for a stamp.
///
/// The width is is determined automatically. Row heights:
/// #table(
/// columns: 2,
/// stroke: none,
/// align: center + horizon,
///
/// text(weight: "semibold")[Without _stamp_],
/// text(weight: "semibold")[With _stamp_],
///
/// table(
/// columns: 3cm,
/// rows: (5mm, 12.7mm, 27.3mm),
/// stroke: 0.5pt + gray,
/// align: center + horizon,
///
/// [_sender_ 5mm],
/// [_annotations_\ 12.7mm],
/// [_recipient_\ 27.3mm],
/// ),
///
/// table(
/// columns: 3cm,
/// rows: (5mm, 21.16mm, 18.84mm),
/// stroke: 0.5pt + gray,
/// align: center + horizon,
///
/// [_sender_ 5mm],
/// [_stamp_ +\ _annotations_\ 21.16mm],
/// [_recipient_\ 18.84mm],
/// )
/// )
///
/// See also: _address-duobox_
///
/// - sender (content, none): The sender box
/// - annotations (content, none): The annotations box
/// - recipient (content, none): The recipient box
/// - stamp (boolean): Enable stamp repartitioning. If enabled, the annotations box and the recipient box divider is moved 8.46mm (about 2 lines) down.
#let address-tribox(sender, annotations, recipient, stamp: false) = {
if stamp {
grid(
columns: 1,
rows: (5mm, 12.7mm + (4.23mm * 2), 27.3mm - (4.23mm * 2)),
sender,
annotations,
recipient,
)
} else {
grid(
columns: 1,
rows: (5mm, 12.7mm, 27.3mm),
sender,
annotations,
recipient,
)
}
}
// #################
// # Simple letter #
// #################
/// This function takes your whole document as its `body` and formats it as a simple letter.
///
/// The default font is set to _Source Sans Pro_ without hyphenation. The body text will be justified.
///
/// - format (string): The format of the letter, which decides the position of the folding marks and the size of the header.
/// #table(
/// columns: (1fr, 1fr, 1fr),
/// stroke: 0.5pt + gray,
///
/// text(weight: "semibold")[Format],
/// text(weight: "semibold")[Folding marks],
/// text(weight: "semibold")[Header size],
///
/// [DIN-5008-A], [87mm, 192mm], [27mm],
/// [DIN-5008-B], [105mm, 210mm], [45mm],
/// )
///
/// - header (auto, content, none): The header that will be displayed at the top of the first page. If header is set to _auto_, a default header will be generaded instead.
/// - footer (content, none): The footer that will be displayed at the bottom of the first page. It automatically grows upwords depending on its body. Make sure to leave enough space in the page margins.
///
/// - folding-marks (boolean): The folding marks that will be displayed at the left margin.
/// - hole-mark (boolean): The hole mark that will be displayed at the left margin.
///
/// - sender (dictionary): The sender that will be displayed below the header on the left.
///
/// The name and address fields must be strings (or none).
///
/// - recipient (content, none): The recipient that will be displayed below the annotations.
///
/// - stamp (boolean): This will increase the annotations box size is by two lines in order to provide more room for the postage stamp that will be displayed below the sender.
/// - annotations (content, none): The annotations box that will be displayed below the sender (or the stamp if enabled).
///
/// - information-box (content, none): The information box that will be displayed below below the header on the right.
/// - reference-signs (array, none): The reference signs that will be displayed below below the the address box. The array has to be a collection of tuples with 2 content elements.
///
/// Example:
/// ```typ
/// (
/// ([Foo], [bar]),
/// ([Hello], [World]),
/// )
/// ```
///
/// - date (content, none): The date that will be displayed on the right below the subject.
/// - subject (string, none): The subject line and the document title.
///
/// - page-numbering (auto, string, function, none): Defines the format of the page numbers.
/// #table(
/// columns: (auto, 1fr),
/// stroke: 0.5pt + gray,
///
/// text(weight: "semibold")[Type], text(weight: "semibold")[Description],
/// [auto], [Automatically determines the document language and chooses an appropriate translation.],
/// [string], [A numbering pattern as specified by the official documentation of the #link("https://typst.app/docs/reference/model/numbering/", text(blue)[_numbering_]) function.],
/// [function], [
/// A function that returns the page number for each page.\
/// Parameters:
/// - current-page (integer)
/// - page-count (integer)
/// Return type: _content_
/// ],
/// [none], [Disable page numbering.],
/// )
///
/// - margin (dictionary): The margin of the letter.
///
/// The dictionary can contain the following fields: _left_, _right_, _top_, _bottom_.\
/// Missing fields will be set to the default.
/// Note: There is no _rest_ field.
///
/// - font (string, array): Font used throughout the letter.
///
/// Keep in mind that some fonts may not be ideal for automated letter processing software
/// and #link("https://en.wikipedia.org/wiki/Optical_character_recognition", text(blue)[OCR]) may fail.
///
/// - body (content, none): The content of the letter
/// -> content
#let letter-simple(
format: "DIN-5008-B",
header: auto,
footer: none,
folding-marks: true,
hole-mark: true,
sender: (
name: none,
address: none,
extra: none,
),
recipient: none,
stamp: false,
annotations: none,
information-box: none,
reference-signs: none,
date: none,
subject: none,
page-numbering: auto,
margin: (
left: 25mm,
right: 20mm,
top: 20mm,
bottom: 20mm,
),
font: "Source Sans Pro",
body,
) = {
margin = (
left: margin.at("left", default: 25mm),
right: margin.at("right", default: 20mm),
top: margin.at("top", default: 20mm),
bottom: margin.at("bottom", default: 20mm),
)
// Configure page and text properties.
set document(
title: subject,
author: sender.name,
)
set text(font: font, hyphenate: false)
// Create a simple header if there is none
if header == auto {
header = pad(
left: margin.left,
right: margin.right,
top: margin.top,
bottom: 5mm,
align(bottom + right, header-simple(
sender.name,
if sender.address != none {
sender.address.split(", ").join(linebreak())
},
extra: sender.at("extra", default: none),
))
)
}
let sender-box = sender-box(name: sender.name, sender.address)
let annotations-box = annotations-box(annotations)
let recipient-box = recipient-box(recipient)
let address-box = address-tribox(sender-box, annotations-box, recipient-box, stamp: stamp)
if (annotations == none) and (stamp == false) {
address-box = address-duobox(align(bottom, pad(bottom: 0.65em, sender-box)), recipient-box)
}
letter-generic(
format: format,
header: header,
footer: footer,
folding-marks: folding-marks,
hole-mark: hole-mark,
address-box: address-box,
information-box: information-box,
reference-signs: reference-signs,
page-numbering: page-numbering,
{
// Add the date line, if any.
if date != none {
align(right, date)
v(0.65em)
}
// Add the subject line, if any.
if subject != none {
pad(right: 10%, strong(subject))
v(0.65em)
}
set par(justify: true)
body
},
margin: margin,
)
}