-
Notifications
You must be signed in to change notification settings - Fork 17
/
lib.typ
796 lines (690 loc) · 20.9 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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
#let _re-num = regex("^(-?\d+(\.|,)?\d*)?(((\+(\d+(\.|,)?\d*)-(\d+(\.|,)?\d*)))|((((\+-)|(-\+))(\d+(\.|,)?\d*))))?((e|E)([-\+]?\d+))?$")
#let _unicode-exponents = (
("\u2070", "0"),
("\u00B9", "1"),
("\u00B2", "2"),
("\u00B3", "3"),
("\u2074", "4"),
("\u2075", "5"),
("\u2076", "6"),
("\u2077", "7"),
("\u2078", "8"),
("\u2079", "9"),
("\u207A", "+"),
("\u207B", "-"),
)
#let _format-float(f, decsep: "auto", thousandsep: "#h(0.166667em)") = {
/// Formats a float with thousands separator.
/// - `f`: Float to format.
/// - `decsep`: Which decimal separator to use. This must be the same as the one used in `f`. Set it to `auto` to automatically choose it. Falls back to `.`.
/// - `thousandsep`: The separator between the thousands.
let string = ""
if decsep == "auto" {
if "," in f {
decsep = ","
} else {
decsep = "."
}
}
if thousandsep.trim() == "." {
thousandsep = ".#h(0mm)"
}
let split = str(f).split(decsep)
let int-part = split.at(0)
let dec-part = split.at(1, default: none)
let int-list = int-part.clusters()
string += str(int-list.remove(0))
for (i, n) in int-list.enumerate() {
let mod = (i - int-list.len()) / 3
if int(mod) == mod {
string += " " + thousandsep + " "
}
string += str(n)
}
if dec-part != none {
let dec-list = dec-part.clusters()
string += decsep
for (i, n) in dec-list.enumerate() {
let mod = i / 3
if int(mod) == mod and i != 0 {
string += " " + thousandsep + " "
}
string += str(n)
}
}
string
}
#let _format-num(
value,
exponent: none,
upper: none,
lower: none,
multiplier: "dot",
thousandsep: "#h(0.166667em)",
) = {
/// Format a number.
/// - `value`: Value of the number.
/// - `exponent`: Exponent in the exponential notation.
/// - `upper`: Upper uncertainty.
/// - `lower`: Lower uncertainty.
/// - `multiplier`: The symbol used to indicate multiplication
/// - `thousandsep`: The separator between the thousands of the float.
let formatted-value = ""
if value != none {
formatted-value += _format-float(value, thousandsep: thousandsep).replace(",", ",#h(0pt)")
}
if upper != none and lower != none {
if upper != lower {
formatted-value += "^(+" + _format-float(upper, thousandsep: thousandsep) + ")"
formatted-value += "_(-" + _format-float(lower, thousandsep: thousandsep) + ")"
} else {
formatted-value += " plus.minus " + _format-float(upper, thousandsep: thousandsep).replace(",", ",#h(0pt)")
}
} else if upper != none {
formatted-value += " plus.minus " + _format-float(upper, thousandsep: thousandsep).replace(",", ",#h(0pt)")
} else if lower != none {
formatted-value += " plus.minus " + _format-float(lower, thousandsep: thousandsep).replace(",", ",#h(0pt)")
}
if not (upper == none and lower == none) {
formatted-value = "lr((" + formatted-value
formatted-value += "))"
}
if exponent != none {
if value != none {
formatted-value += " " + multiplier + " "
}
formatted-value += "10^(" + str(exponent) + ")"
}
formatted-value
}
#let num(value, multiplier: "dot", thousandsep: "#h(0.166667em)") = {
/// Format a number.
/// - `value`: String with the number.
/// - `multiplier`: The symbol used to indicate multiplication
/// - `thousandsep`: The separator between the thousands of the float.
// str() converts minus "-" of a number to unicode "\u2212"
value = str(value).replace("−", "-").replace(" ", "") //.replace(",", ".")
let match-value = value.match(_re-num)
assert.ne(match-value, none, message: "invalid number: " + value)
let captures-value = match-value.captures
let upper = none
let lower = none
if captures-value.at(14) != none {
upper = captures-value.at(14)
lower = none
} else {
upper = captures-value.at(5)
lower = captures-value.at(7)
}
let formatted = _format-num(
captures-value.at(0),
exponent: captures-value.at(18),
upper: upper,
lower: lower,
multiplier: multiplier,
thousandsep: thousandsep,
)
formatted = "$" + formatted + "$"
eval(formatted)
}
#let _prefix-csv(path, delimiter: ",") = {
/// Load a CSV file with pre- or postfixes.
/// - `path`: Path of the CSV file.
/// - `delimiter`: Passed to the `csv` function.
let array = csv(path, delimiter: delimiter)
let symbols = (:)
let symbols-short = (:)
for line in array {
symbols.insert(lower(line.at(0)), line.at(2))
symbols-short.insert(line.at(1), line.at(2))
}
(symbols, symbols-short)
}
#let _postfix-csv(path, delimiter: ",") = {
/// Load a CSV file with pre- or postfixes.
/// - `path`: Path of the CSV file.
/// - `delimiter`: Passed to the `csv` function.
let array = csv(path, delimiter: delimiter)
let dict = (:)
for line in array {
dict.insert(lower(line.at(0)), line.at(1))
}
dict
}
#let _unit-csv(path, delimiter: ",") = {
/// Load a CSV file with units.
/// - `path`: Path of the CSV file.
/// - `delimiter`: Passed to the `csv` function.
let array = csv(path, delimiter: delimiter)
let units = (:)
let units-short = (:)
let units-space = (:)
let units-short-space = (:)
for line in array {
units.insert(lower(line.at(0)), line.at(2))
units-short.insert(line.at(1), line.at(2))
if line.at(3) == "false" or line.at(3) == "0" {
units-space.insert(lower(line.at(0)), false)
units-short-space.insert(line.at(1), false)
} else {
units-space.insert(lower(line.at(0)), true)
units-short-space.insert(line.at(1), true)
}
}
(units, units-short, units-space, units-short-space)
}
#let _add-money-units(data) = {
let (units, units-short, units-space, units-short-space) = data
let array = csv("money.csv", delimiter: ",")
for line in array {
units.insert(lower(line.at(0)), line.at(2))
units-short.insert(line.at(1), line.at(2))
if line.at(3) == "false" or line.at(3) == "0" {
units-space.insert(lower(line.at(0)), false)
units-short-space.insert(line.at(1), false)
} else {
units-space.insert(lower(line.at(0)), true)
units-short-space.insert(line.at(1), true)
}
}
(units, units-short, units-space, units-short-space)
}
#let postfixes = _postfix-csv("postfixes.csv")
#let lang-db = state(
"lang-db",
(
"en": (
"units": (_unit-csv("units-en.csv")),
"prefixes": (_prefix-csv("prefixes-en.csv")),
),
"ru": (
"units": (_unit-csv("units-ru.csv")),
"prefixes": (_prefix-csv("prefixes-ru.csv")),
),
),
)
// get units
#let __inits() = {
let lang = text.lang
let data = lang-db.get()
if lang in data {
_add-money-units(data.at(lang).units)
} else {
_add-money-units(data.en.units)
}
}
// get prefixes
#let __prefixes() = {
let lang = text.lang
let data = lang-db.get()
if lang in data {
data.at(lang).prefixes
} else {
data.en.prefixes
}
}
#let _unicode-exponent-list = for (unicode, ascii) in _unicode-exponents {
(unicode,)
}
#let _exponent-pattern = regex("[" + _unicode-exponent-list.join("|") + "]+")
#let _replace-unicode-exponents(unit-str) = {
let exponent-matches = unit-str.matches(_exponent-pattern)
let exponent = ""
for match in exponent-matches {
exponent = "^" + match.text
for (unicode, ascii) in _unicode-exponents {
exponent = exponent.replace(regex(unicode), ascii)
}
unit-str = unit-str.replace(match.text, exponent)
}
unit-str
}
#let chunk(string, cond) = (string: string, cond: cond)
#let _format-unit-short(
string,
space: "#h(0.166667em)",
per: "symbol",
units-short,
units-short-space,
prefixes-short,
) = {
/// Format a unit using the shorthand notation.
/// - `string`: String containing the unit.
/// - `space`: Space between units.
/// - `per`: Whether to format the units after `/` with a fraction or exponent.
assert(per == "symbol" or per == "fraction" or per == "/")
let formatted = ""
string = _replace-unicode-exponents(string)
let split = string.replace(regex(" */ *"), "/").replace(regex(" +"), " ").split(regex(" "))
let chunks = ()
for s in split {
let per-split = s.split("/")
chunks.push(chunk(per-split.at(0), false))
if per-split.len() > 1 {
for p in per-split.slice(1) {
chunks.push(chunk(p, true))
}
}
}
// needed for fraction formatting
let normal-list = ()
let per-list = ()
let prefixes = ()
for (string: string, cond: per-set) in chunks {
let u-space = true
let prefix = none
let unit = ""
let exponent = none
let qty-exp = string.split("^")
let quantity = qty-exp.at(0)
exponent = qty-exp.at(1, default: none)
if quantity in units-short {
// Match units without prefixes
unit = units-short.at(quantity)
u-space = units-short-space.at(quantity)
} else {
// Match prefix + unit
let pre = ""
for char in quantity.clusters() {
pre += char
// Divide `quantity` into `pre`+`u` and check validity
if pre in prefixes-short {
let u = quantity.trim(pre, at: start, repeat: false)
if u in units-short {
prefix = prefixes-short.at(pre)
unit = units-short.at(u)
u-space = units-short-space.at(u)
pre = none
break
}
}
}
if pre != none {
panic("invalid unit: " + quantity)
}
}
if per == "symbol" {
if u-space {
formatted += space
}
formatted += prefix + unit
if exponent != none {
if per-set {
formatted += "^(-" + exponent + ")"
} else {
formatted += "^(" + exponent + ")"
}
} else if per-set {
formatted += "^(-1)"
}
} else {
let final-unit = ""
// if u-space {
// final-unit += space
// }
final-unit += prefix + unit
if exponent != none {
final-unit += "^(" + exponent + ")"
}
if per-set {
per-list.push(chunk(final-unit, u-space))
} else {
normal-list.push(chunk(final-unit, u-space))
}
}
}
// return((normal-list, per-list))
if per != "symbol" {
if normal-list.at(0).at("cond") {
formatted += space
}
if per-list.len() > 0 {
formatted += " ("
}
for (i, chunk) in normal-list.enumerate() {
let (string: n, cond: space-set) = chunk
if i != 0 and space-set {
formatted += space
}
formatted += n
}
if per-list.len() == 0 {
return formatted
}
formatted += ")/("
for (i, chunk) in per-list.enumerate() {
let (string: p, cond: space-set) = chunk
if i != 0 and space-set {
formatted += space
}
formatted += p
}
formatted += ")"
}
formatted
}
#let _format-unit(string, space: "#h(0.166667em)", per: "symbol") = {
/// Format a unit using written-out words.
/// - `string`: String containing the unit.
/// - `space`: Space between units.
/// - `per`: Whether to format the units after `per` with a fraction or exponent.
assert(per == "symbol" or per == "fraction" or per == "/")
// load data
let (units, units-short, units-space, units-short-space) = __inits()
let (prefixes, prefixes-short) = __prefixes()
let formatted = ""
// needed for fraction formatting
let normal-list = ()
let per-list = ()
// whether per was used
let per-set = false
// whether waiting for a postfix
let post = false
// one unit
let unit = chunk("", true)
let split = lower(string).split(" ")
split.push("")
for u in split {
// expecting postfix
if post {
if per == "symbol" {
// add postfix
if u in postfixes {
if per-set {
unit.at("string") += "^(-"
} else {
unit.at("string") += "^("
}
unit.at("string") += postfixes.at(u)
unit.at("string") += ")"
if unit.at("cond") {
unit.at("string") = space + unit.at("string")
}
per-set = false
post = false
formatted += unit.at("string")
unit = chunk("", true)
continue
// add per
} else if per-set {
unit.at("string") += "^(-1)"
if unit.at("cond") {
unit.at("string") = space + unit.at("string")
}
per-set = false
post = false
formatted += unit.at("string")
unit = chunk("", true)
// finish unit
} else {
post = false
if unit.at("cond") {
unit.at("string") = space + unit.at("string")
}
formatted += unit.at("string")
unit = chunk("", true)
}
} else {
if u in postfixes {
unit.at("string") += "^("
unit.at("string") += postfixes.at(u)
unit.at("string") += ")"
if per-set {
per-list.push(unit)
} else {
normal-list.push(unit)
}
per-set = false
post = false
unit = chunk("", true)
continue
} else {
if per-set {
per-list.push(unit)
} else {
normal-list.push(unit)
}
per-set = false
post = false
unit = chunk("", true)
}
}
}
// detected per
if u == "per" {
per-set = true
// add prefix
} else if u in prefixes {
unit.at("string") += prefixes.at(u)
// add unit
} else if u in units {
unit.at("string") += units.at(u)
unit.at("cond") = units-space.at(u)
post = true
} else if u != "" {
return _format-unit-short(string, space: space, per: per, units-short, units-short-space, prefixes-short)
}
}
if per != "symbol" {
if normal-list.at(0).at("cond") {
formatted += space
}
if per-list.len() > 0 {
formatted += " ("
}
for (i, chunk) in normal-list.enumerate() {
let (string: n, cond: space-set) = chunk
if i != 0 and space-set {
formatted += space
}
formatted += n
}
if per-list.len() == 0 {
return formatted
}
formatted += ")/("
for (i, chunk) in per-list.enumerate() {
let (string: p, cond: space-set) = chunk
if i != 0 and space-set {
formatted += space
}
formatted += p
}
formatted += ")"
}
formatted
}
#let unit(unit, space: "#h(0.166667em)", per: "symbol") = {
/// Format a unit.
/// - `unit`: String containing the unit.
/// - `space`: Space between units.
/// - `per`: Whether to format the units after `per` or `/` with a fraction or exponent.
context {
let formatted-unit = ""
formatted-unit = _format-unit(unit, space: space, per: per)
let formatted = "$" + formatted-unit + "$"
eval(formatted)
}
}
#let qty(
value,
unit,
rawunit: false,
space: "#h(0.166667em)",
multiplier: "dot",
thousandsep: "#h(0.166667em)",
per: "symbol",
) = {
/// Format a quantity (i.e. number with a unit).
/// - `value`: String containing the number.
/// - `unit`: String containing the unit.
/// - `multiplier`: The symbol used to indicate multiplication
/// - `rawunit`: Whether to transform the unit or keep the raw string.
/// - `space`: Space between units.
/// - `thousandsep`: The separator between the thousands of the float.
/// - `per`: Whether to format the units after `per` or `/` with a fraction or exponent.
value = str(value).replace("−", "-").replace(" ", "")
let match-value = value.match(_re-num)
assert.ne(match-value, none, message: "invalid number: " + value)
let captures-value = match-value.captures
let upper = none
let lower = none
if captures-value.at(14) != none {
upper = captures-value.at(14)
lower = none
} else {
upper = captures-value.at(5)
lower = captures-value.at(7)
}
let formatted-value = _format-num(
captures-value.at(0),
exponent: captures-value.at(18),
upper: upper,
lower: lower,
multiplier: multiplier,
thousandsep: thousandsep,
)
context {
let formatted-unit = ""
if rawunit {
formatted-unit = space + unit
} else {
formatted-unit = _format-unit(unit, space: space, per: per)
}
let formatted = "$" + formatted-value + formatted-unit + "$"
eval(formatted)
}
}
#let _format-range(
lower,
upper,
exponent-lower: none,
exponent-upper: none,
multiplier: "dot",
delimiter: "-",
space: "#h(0.16667em)",
thousandsep: "#h(0.166667em)",
force-parentheses: false,
) = {
/// Format a range.
/// - `(lower, upper)`: Strings containing the numbers.
/// - `(exponent-lower, exponent-upper)`: Strings containing the exponentials in exponential notation.
/// - `multiplier`: The symbol used to indicate multiplication
/// - `delimiter`: Symbol between the numbers.
/// - `space`: Space between the numbers and the delimiter.
/// - `thousandsep`: The separator between the thousands of the float.
/// - `force-parentheses`: Whether to force parentheses around the range.
let formatted-value = ""
formatted-value += _format-num(lower, thousandsep: thousandsep).replace(",", ",#h(0pt)")
if exponent-lower != exponent-upper and exponent-lower != none {
if lower != none {
formatted-value += multiplier + " "
}
formatted-value += "10^(" + str(exponent-lower) + ")"
}
formatted-value += space + " " + delimiter + " " + space + _format-num(upper, thousandsep: thousandsep).replace(
",",
",#h(0pt)",
)
if exponent-lower != exponent-upper and exponent-upper != none {
if upper != none {
formatted-value += multiplier + " "
}
formatted-value += "10^(" + str(exponent-upper) + ")"
}
if exponent-lower == exponent-upper and (exponent-lower != none and exponent-upper != none) {
formatted-value = "lr((" + formatted-value
formatted-value += ")) " + multiplier + " 10^(" + str(exponent-lower) + ")"
} else if force-parentheses {
formatted-value = "lr((" + formatted-value
formatted-value += "))"
}
formatted-value
}
#let numrange(
lower,
upper,
multiplier: "dot",
delimiter: "-",
space: "#h(0.16667em)",
thousandsep: "#h(0.166667em)",
) = {
/// Format a range.
/// - `(lower, upper)`: Strings containing the numbers.
/// - `multiplier`: The symbol used to indicate multiplication
/// - `delimiter`: Symbol between the numbers.
/// - `space`: Space between the numbers and the delimiter.
/// - `thousandsep`: The separator between the thousands of the float.
lower = str(lower).replace("−", "-").replace(" ", "")
let match-lower = lower.match(_re-num)
assert.ne(match-lower, none, message: "invalid lower number: " + lower)
let captures-lower = match-lower.captures
upper = str(upper).replace("−", "-").replace(" ", "")
let match-upper = upper.match(_re-num)
assert.ne(match-upper, none, message: "invalid upper number: " + upper)
let captures-upper = match-upper.captures
let formatted = _format-range(
captures-lower.at(0),
captures-upper.at(0),
exponent-lower: captures-lower.at(18),
exponent-upper: captures-upper.at(18),
multiplier: multiplier,
delimiter: delimiter,
thousandsep: thousandsep,
space: space,
)
formatted = "$" + formatted + "$"
eval(formatted)
}
#let qtyrange(
lower,
upper,
unit,
rawunit: false,
multiplier: "dot",
delimiter: "-",
space: "",
unitspace: "#h(0.16667em)",
thousandsep: "#h(0.166667em)",
per: "symbol",
) = {
/// Format a range with a unit.
/// - `(lower, upper)`: Strings containing the numbers.
/// - `unit`: String containing the unit.
/// - `rawunit`: Whether to transform the unit or keep the raw string.
/// - `multiplier`: The symbol used to indicate multiplication
/// - `delimiter`: Symbol between the numbers.
/// - `space`: Space between the numbers and the delimiter.
/// - `unitspace`: Space between units.
/// - `thousandsep`: The separator between the thousands of the float.
/// - `per`: Whether to format the units after `per` or `/` with a fraction or exponent.
lower = str(lower).replace("−", "-").replace(" ", "")
let match-lower = lower.match(_re-num)
assert.ne(match-lower, none, message: "invalid lower number: " + lower)
let captures-lower = match-lower.captures
upper = str(upper).replace("−", "-").replace(" ", "")
let match-upper = upper.match(_re-num)
assert.ne(match-upper, none, message: "invalid upper number: " + upper)
let captures-upper = match-upper.captures
let formatted-value = _format-range(
captures-lower.at(0),
captures-upper.at(0),
exponent-lower: captures-lower.at(18),
exponent-upper: captures-upper.at(18),
multiplier: multiplier,
delimiter: delimiter,
space: space,
thousandsep: thousandsep,
force-parentheses: true,
)
context {
let formatted-unit = ""
if rawunit {
formatted-unit = space + unit
} else {
formatted-unit = _format-unit(unit, space: unitspace, per: per)
}
let formatted = "$" + formatted-value + formatted-unit + "$"
eval(formatted)
}
}