-
Notifications
You must be signed in to change notification settings - Fork 18
/
lib.typ
240 lines (210 loc) · 7.01 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
#import "format.typ": *
#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 add-unit(unit, shorthand, symbol, space: true) = {
/// Add a new unit.
/// - `unit`: Full name of the unit.
/// - `shorthand`: Shorthand of the unit, usually only 1-2 letters.
/// - `symbol`: String that will be inserted as the unit symbol.
/// - `space`: Whether to put a space before the unit.
context {
let lang = _get-language()
_lang-db.update(db => {
db.at(lang).at("units").at(0).insert(unit, symbol)
db.at(lang).at("units").at(1).insert(shorthand, symbol)
db.at(lang).at("units").at(2).insert(unit, space)
db.at(lang).at("units").at(3).insert(shorthand, space)
db
})
}
}
#let add-prefix(prefix, shorthand, symbol) = {
/// Add a new prefix.
/// - `prefix`: Full name of the prefix.
/// - `shorthand`: Shorthand of the prefix, usually only 1-2 letters.
/// - `symbol`: String that will be inserted as the prefix symbol.
context {
let lang = _get-language()
_lang-db.update(db => {
db.at(lang).at("prefixes").at(0).insert(prefix, symbol)
db.at(lang).at("prefixes").at(1).insert(shorthand, symbol)
db
})
}
}
#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 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)
}
}