@zyro-sec/latex 0.0.0-stage → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +26 -4
- package/src/layout.test.ts +87 -0
- package/src/layout.ts +673 -0
- package/src/limits.ts +40 -0
- package/src/markdown.test.ts +267 -0
- package/src/markdown.ts +116 -0
- package/src/parser-render.test.ts +23 -0
- package/src/parser.test.ts +298 -0
- package/src/parser.ts +598 -0
- package/src/plugin.ts +14 -0
- package/src/render.test.ts +87 -0
- package/src/render.ts +11 -0
- package/src/root.test.ts +69 -0
- package/src/symbols.ts +315 -0
- package/src/types.ts +97 -0
- package/tsconfig.json +10 -0
- package/README.md +0 -3
package/src/layout.ts
ADDED
|
@@ -0,0 +1,673 @@
|
|
|
1
|
+
import type { MathCell, MathLayout, MathNode, MathStyle, MathVariant, RenderLatexOptions, SymbolRole } from "./types"
|
|
2
|
+
|
|
3
|
+
interface Box {
|
|
4
|
+
width: number
|
|
5
|
+
height: number
|
|
6
|
+
baseline: number
|
|
7
|
+
cells: Array<Array<MathCell | undefined>>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface LayoutContext {
|
|
11
|
+
displayMode: boolean
|
|
12
|
+
compactScripts: boolean
|
|
13
|
+
style?: MathStyle
|
|
14
|
+
variant?: MathVariant
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" })
|
|
18
|
+
|
|
19
|
+
const superscripts: Readonly<Record<string, string>> = {
|
|
20
|
+
"0": "⁰",
|
|
21
|
+
"1": "¹",
|
|
22
|
+
"2": "²",
|
|
23
|
+
"3": "³",
|
|
24
|
+
"4": "⁴",
|
|
25
|
+
"5": "⁵",
|
|
26
|
+
"6": "⁶",
|
|
27
|
+
"7": "⁷",
|
|
28
|
+
"8": "⁸",
|
|
29
|
+
"9": "⁹",
|
|
30
|
+
"+": "⁺",
|
|
31
|
+
"-": "⁻",
|
|
32
|
+
"=": "⁼",
|
|
33
|
+
"(": "⁽",
|
|
34
|
+
")": "⁾",
|
|
35
|
+
n: "ⁿ",
|
|
36
|
+
i: "ⁱ",
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const subscripts: Readonly<Record<string, string>> = {
|
|
40
|
+
"0": "₀",
|
|
41
|
+
"1": "₁",
|
|
42
|
+
"2": "₂",
|
|
43
|
+
"3": "₃",
|
|
44
|
+
"4": "₄",
|
|
45
|
+
"5": "₅",
|
|
46
|
+
"6": "₆",
|
|
47
|
+
"7": "₇",
|
|
48
|
+
"8": "₈",
|
|
49
|
+
"9": "₉",
|
|
50
|
+
"+": "₊",
|
|
51
|
+
"-": "₋",
|
|
52
|
+
"=": "₌",
|
|
53
|
+
"(": "₍",
|
|
54
|
+
")": "₎",
|
|
55
|
+
a: "ₐ",
|
|
56
|
+
e: "ₑ",
|
|
57
|
+
h: "ₕ",
|
|
58
|
+
i: "ᵢ",
|
|
59
|
+
j: "ⱼ",
|
|
60
|
+
k: "ₖ",
|
|
61
|
+
l: "ₗ",
|
|
62
|
+
m: "ₘ",
|
|
63
|
+
n: "ₙ",
|
|
64
|
+
o: "ₒ",
|
|
65
|
+
p: "ₚ",
|
|
66
|
+
r: "ᵣ",
|
|
67
|
+
s: "ₛ",
|
|
68
|
+
t: "ₜ",
|
|
69
|
+
u: "ᵤ",
|
|
70
|
+
v: "ᵥ",
|
|
71
|
+
x: "ₓ",
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function layoutMath(node: MathNode, options: RenderLatexOptions = {}): MathLayout {
|
|
75
|
+
const context: LayoutContext = {
|
|
76
|
+
displayMode: options.displayMode ?? true,
|
|
77
|
+
compactScripts: options.compactScripts ?? true,
|
|
78
|
+
...(options.color ? { style: { color: options.color } } : {}),
|
|
79
|
+
}
|
|
80
|
+
return asPublicLayout(layoutNode(node, context))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function layoutNode(node: MathNode, context: LayoutContext): Box {
|
|
84
|
+
switch (node.type) {
|
|
85
|
+
case "row":
|
|
86
|
+
return layoutRow(node.body, context)
|
|
87
|
+
case "symbol":
|
|
88
|
+
case "text":
|
|
89
|
+
case "operator":
|
|
90
|
+
return textBox(applyVariant(node.value, context.variant), context.style)
|
|
91
|
+
case "space":
|
|
92
|
+
return blank(node.width, 1, 0)
|
|
93
|
+
case "fraction":
|
|
94
|
+
return layoutFraction(node, context)
|
|
95
|
+
case "root":
|
|
96
|
+
return layoutRoot(node.body, node.index, context)
|
|
97
|
+
case "scripts":
|
|
98
|
+
return layoutScripts(node, context)
|
|
99
|
+
case "delimited":
|
|
100
|
+
return layoutDelimited(node.left, node.body, node.right, context)
|
|
101
|
+
case "matrix":
|
|
102
|
+
return layoutMatrix(node, context)
|
|
103
|
+
case "brace":
|
|
104
|
+
return layoutBrace(node, context)
|
|
105
|
+
case "accent":
|
|
106
|
+
return layoutAccent(node.accent, node.body, context)
|
|
107
|
+
case "variant":
|
|
108
|
+
return layoutNode(node.body, withVariant(context, node.variant))
|
|
109
|
+
case "overunder":
|
|
110
|
+
return layoutOverUnder(node.base, node.over, node.under, context)
|
|
111
|
+
case "color":
|
|
112
|
+
return layoutNode(node.body, { ...context, style: { ...context.style, color: node.color } })
|
|
113
|
+
}
|
|
114
|
+
throw new Error("Unsupported math node")
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function layoutRow(nodes: MathNode[], context: LayoutContext): Box {
|
|
118
|
+
if (nodes.length === 0) return blank(0, 1, 0)
|
|
119
|
+
|
|
120
|
+
const boxes: Box[] = []
|
|
121
|
+
let previousRole: SymbolRole | undefined
|
|
122
|
+
|
|
123
|
+
for (let index = 0; index < nodes.length; index++) {
|
|
124
|
+
const node = nodes[index]
|
|
125
|
+
const rawRole = nodeRole(node)
|
|
126
|
+
const role = normalizeBinaryRole(rawRole, previousRole, nextSignificantRole(nodes, index + 1))
|
|
127
|
+
if (needsMathSpace(previousRole, role, boxes.length)) boxes.push(blank(1, 1, 0))
|
|
128
|
+
boxes.push(layoutNode(node, context))
|
|
129
|
+
if (node.type !== "space") previousRole = role ?? "ordinary"
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return hpack(boxes)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function layoutFraction(node: Extract<MathNode, { type: "fraction" }>, context: LayoutContext): Box {
|
|
136
|
+
const numerator = layoutNode(node.numerator, context)
|
|
137
|
+
const denominator = layoutNode(node.denominator, context)
|
|
138
|
+
const width = Math.max(numerator.width, denominator.width) + 2
|
|
139
|
+
// Barless fractions (binomials) still reserve an axis row so surrounding
|
|
140
|
+
// atoms and their stretching parentheses align between the two entries.
|
|
141
|
+
const gap = 1
|
|
142
|
+
const height = numerator.height + denominator.height + gap
|
|
143
|
+
// TeX places a fraction's math axis on its rule (or the equivalent empty
|
|
144
|
+
// axis row for a binomial). Align neighbors there, not on the denominator.
|
|
145
|
+
const baseline = numerator.height
|
|
146
|
+
const result = blank(width, height, baseline)
|
|
147
|
+
|
|
148
|
+
const numeratorX =
|
|
149
|
+
node.numeratorAlign === "left"
|
|
150
|
+
? 1
|
|
151
|
+
: node.numeratorAlign === "right"
|
|
152
|
+
? width - numerator.width - 1
|
|
153
|
+
: Math.floor((width - numerator.width) / 2)
|
|
154
|
+
overlay(result, numerator, numeratorX, 0)
|
|
155
|
+
if (node.bar) drawHorizontal(result, numerator.height, 0, width, "─", context.style)
|
|
156
|
+
overlay(result, denominator, Math.floor((width - denominator.width) / 2), numerator.height + gap)
|
|
157
|
+
return result
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function layoutRoot(bodyNode: MathNode, indexNode: MathNode | undefined, context: LayoutContext): Box {
|
|
161
|
+
const body = layoutNode(bodyNode, context)
|
|
162
|
+
const index = indexNode ? layoutNode(indexNode, context) : undefined
|
|
163
|
+
const indexWidth = index ? Math.max(0, index.width - 1) : 0
|
|
164
|
+
// The index ends beside the overbar, never inside the hook or radicand.
|
|
165
|
+
const top = Math.max(0, (index?.height ?? 1) - 1)
|
|
166
|
+
const bodyX = indexWidth + 2
|
|
167
|
+
const width = bodyX + body.width
|
|
168
|
+
const height = top + body.height + 1
|
|
169
|
+
const baseline = top + body.baseline + 1
|
|
170
|
+
const result = blank(width, height, baseline)
|
|
171
|
+
|
|
172
|
+
setCell(result, bodyX - 1, top, "╭", context.style)
|
|
173
|
+
drawHorizontal(result, top, bodyX, body.width, "─", context.style)
|
|
174
|
+
for (let y = top + 1; y < height - 1; y++) setCell(result, bodyX - 1, y, "│", context.style)
|
|
175
|
+
setCell(result, bodyX - 2, height - 1, "╰", context.style)
|
|
176
|
+
setCell(result, bodyX - 1, height - 1, "╯", context.style)
|
|
177
|
+
overlay(result, body, bodyX, top + 1)
|
|
178
|
+
if (index) overlay(result, index, 0, 0)
|
|
179
|
+
return result
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function layoutScripts(node: Extract<MathNode, { type: "scripts" }>, context: LayoutContext): Box {
|
|
183
|
+
const superscriptNode = node.superscript && simpleNodeText(node.superscript) !== "" ? node.superscript : undefined
|
|
184
|
+
const subscriptNode = node.subscript && simpleNodeText(node.subscript) !== "" ? node.subscript : undefined
|
|
185
|
+
if (node.base.type === "brace" || (node.base.type === "operator" && node.base.limits && context.displayMode)) {
|
|
186
|
+
return layoutOverUnder(node.base, superscriptNode, subscriptNode, context)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const base = layoutNode(node.base, context)
|
|
190
|
+
if (context.compactScripts && base.height === 1) {
|
|
191
|
+
const superscript = mapScript(superscriptNode ? simpleNodeText(superscriptNode) : "", superscripts)
|
|
192
|
+
const subscript = mapScript(subscriptNode ? simpleNodeText(subscriptNode) : "", subscripts)
|
|
193
|
+
if (superscript !== undefined && subscript !== undefined) {
|
|
194
|
+
return hpack([base, textBox(superscript + subscript, context.style)])
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const superscript = superscriptNode ? layoutNode(superscriptNode, context) : undefined
|
|
199
|
+
const subscript = subscriptNode ? layoutNode(subscriptNode, context) : undefined
|
|
200
|
+
const scriptWidth = Math.max(superscript?.width ?? 0, subscript?.width ?? 0)
|
|
201
|
+
const topHeight = superscript?.height ?? 0
|
|
202
|
+
const bottomHeight = subscript?.height ?? 0
|
|
203
|
+
const width = base.width + scriptWidth
|
|
204
|
+
const height = topHeight + base.height + bottomHeight
|
|
205
|
+
const baseline = topHeight + base.baseline
|
|
206
|
+
const result = blank(width, height, baseline)
|
|
207
|
+
|
|
208
|
+
overlay(result, base, 0, topHeight)
|
|
209
|
+
if (superscript) overlay(result, superscript, base.width, 0)
|
|
210
|
+
if (subscript) overlay(result, subscript, base.width, topHeight + base.height)
|
|
211
|
+
return result
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function layoutOverUnder(
|
|
215
|
+
baseNode: MathNode,
|
|
216
|
+
overNode: MathNode | undefined,
|
|
217
|
+
underNode: MathNode | undefined,
|
|
218
|
+
context: LayoutContext,
|
|
219
|
+
): Box {
|
|
220
|
+
const base = layoutNode(baseNode, context)
|
|
221
|
+
const over = overNode ? layoutNode(overNode, context) : undefined
|
|
222
|
+
const under = underNode ? layoutNode(underNode, context) : undefined
|
|
223
|
+
const width = Math.max(base.width, over?.width ?? 0, under?.width ?? 0)
|
|
224
|
+
const overHeight = over?.height ?? 0
|
|
225
|
+
const height = overHeight + base.height + (under?.height ?? 0)
|
|
226
|
+
const baseline = overHeight + base.baseline
|
|
227
|
+
const result = blank(width, height, baseline)
|
|
228
|
+
|
|
229
|
+
if (over) overlay(result, over, Math.floor((width - over.width) / 2), 0)
|
|
230
|
+
overlay(result, base, Math.floor((width - base.width) / 2), overHeight)
|
|
231
|
+
if (under) overlay(result, under, Math.floor((width - under.width) / 2), overHeight + base.height)
|
|
232
|
+
return result
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function layoutDelimited(left: string, bodyNode: MathNode, right: string, context: LayoutContext): Box {
|
|
236
|
+
const body = layoutNode(bodyNode, context)
|
|
237
|
+
const leftBox = delimiterBox(left, body.height, body.baseline, true, context.style)
|
|
238
|
+
const rightBox = delimiterBox(right, body.height, body.baseline, false, context.style)
|
|
239
|
+
return hpack([leftBox, body, rightBox])
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function layoutMatrix(node: Extract<MathNode, { type: "matrix" }>, context: LayoutContext): Box {
|
|
243
|
+
const cellRows = node.rows.map((row) => row.map((cell) => layoutNode(cell, context)))
|
|
244
|
+
const columns = node.columns?.match(/[lcr]/g)
|
|
245
|
+
const rules = node.columns?.split(/[lcr]/).map((rule) => rule.length) ?? []
|
|
246
|
+
const columnCount = Math.max(columns?.length ?? 0, ...cellRows.map((row) => row.length))
|
|
247
|
+
const columnWidths = Array.from({ length: columnCount }, (_, column) =>
|
|
248
|
+
Math.max(0, ...cellRows.map((row) => row[column]?.width ?? 0)),
|
|
249
|
+
)
|
|
250
|
+
const rowAscents = cellRows.map((row) => Math.max(0, ...row.map((cell) => cell.baseline)))
|
|
251
|
+
const rowDescents = cellRows.map((row) => Math.max(0, ...row.map((cell) => cell.height - cell.baseline - 1)))
|
|
252
|
+
const rowHeights = rowAscents.map((ascent, index) => ascent + 1 + rowDescents[index])
|
|
253
|
+
const aligned = node.environment === "aligned" || node.environment === "align"
|
|
254
|
+
const columnGap = node.environment === "cases" || aligned ? 2 : 1
|
|
255
|
+
const gaps = Array.from({ length: columnCount + 1 }, (_, boundary) => {
|
|
256
|
+
const edge = boundary === 0 || boundary === columnCount
|
|
257
|
+
return rules[boundary] ? rules[boundary] + (edge ? 1 : 2) : edge ? 0 : columnGap
|
|
258
|
+
})
|
|
259
|
+
const width = columnWidths.reduce((sum, value) => sum + value, 0) + gaps.reduce((sum, value) => sum + value, 0)
|
|
260
|
+
const height = Math.max(1, rowHeights.reduce((sum, value) => sum + value, 0) + Math.max(0, node.rows.length - 1))
|
|
261
|
+
const result = blank(width, height, Math.floor(height / 2))
|
|
262
|
+
let y = 0
|
|
263
|
+
|
|
264
|
+
for (let rowIndex = 0; rowIndex < cellRows.length; rowIndex++) {
|
|
265
|
+
let x = gaps[0]
|
|
266
|
+
const cells = cellRows[rowIndex]
|
|
267
|
+
for (let column = 0; column < columnCount; column++) {
|
|
268
|
+
const cell = cells[column]
|
|
269
|
+
const columnWidth = columnWidths[column]
|
|
270
|
+
if (cell) {
|
|
271
|
+
const alignment =
|
|
272
|
+
columns?.[column] ?? (node.environment === "cases" ? "l" : aligned ? (column % 2 === 0 ? "r" : "l") : "c")
|
|
273
|
+
const cellX =
|
|
274
|
+
x +
|
|
275
|
+
(alignment === "l"
|
|
276
|
+
? 0
|
|
277
|
+
: alignment === "r"
|
|
278
|
+
? columnWidth - cell.width
|
|
279
|
+
: Math.floor((columnWidth - cell.width) / 2))
|
|
280
|
+
const cellY = y + rowAscents[rowIndex] - cell.baseline
|
|
281
|
+
overlay(result, cell, cellX, cellY)
|
|
282
|
+
}
|
|
283
|
+
x += columnWidth + gaps[column + 1]
|
|
284
|
+
}
|
|
285
|
+
y += rowHeights[rowIndex] + 1
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
let boundaryX = 0
|
|
289
|
+
for (let boundary = 0; boundary <= columnCount; boundary++) {
|
|
290
|
+
for (let rule = 0; rule < (rules[boundary] ?? 0); rule++) {
|
|
291
|
+
for (let row = 0; row < height; row++) {
|
|
292
|
+
setCell(result, boundaryX + (boundary === 0 ? 0 : 1) + rule, row, "│", context.style)
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
boundaryX += gaps[boundary] + (columnWidths[boundary] ?? 0)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const delimiters = matrixDelimiters(node.environment)
|
|
299
|
+
return delimiters
|
|
300
|
+
? hpack([
|
|
301
|
+
delimiterBox(delimiters[0], height, result.baseline, true, context.style),
|
|
302
|
+
result,
|
|
303
|
+
delimiterBox(delimiters[1], height, result.baseline, false, context.style),
|
|
304
|
+
])
|
|
305
|
+
: result
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function layoutBrace(node: Extract<MathNode, { type: "brace" }>, context: LayoutContext): Box {
|
|
309
|
+
const body = layoutNode(node.body, context)
|
|
310
|
+
const over = node.position === "over"
|
|
311
|
+
const width = Math.max(3, body.width)
|
|
312
|
+
const result = blank(width, body.height + 1, body.baseline + (over ? 1 : 0))
|
|
313
|
+
const y = over ? 0 : body.height
|
|
314
|
+
overlay(result, body, Math.floor((width - body.width) / 2), over ? 1 : 0)
|
|
315
|
+
drawHorizontal(result, y, 0, width, "─", context.style)
|
|
316
|
+
setCell(result, 0, y, over ? "╭" : "╰", context.style)
|
|
317
|
+
setCell(result, width - 1, y, over ? "╮" : "╯", context.style)
|
|
318
|
+
setCell(result, Math.floor((width - 1) / 2), y, over ? "┴" : "┬", context.style)
|
|
319
|
+
return result
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function layoutAccent(
|
|
323
|
+
accent: Extract<MathNode, { type: "accent" }>["accent"],
|
|
324
|
+
bodyNode: MathNode,
|
|
325
|
+
context: LayoutContext,
|
|
326
|
+
): Box {
|
|
327
|
+
const body = layoutNode(bodyNode, context)
|
|
328
|
+
if (accent === "underline") {
|
|
329
|
+
const result = blank(body.width, body.height + 1, body.baseline)
|
|
330
|
+
overlay(result, body, 0, 0)
|
|
331
|
+
drawHorizontal(result, body.height, 0, body.width, "─", context.style)
|
|
332
|
+
return result
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const result = blank(body.width, body.height + 1, body.baseline + 1)
|
|
336
|
+
overlay(result, body, 0, 1)
|
|
337
|
+
const mark =
|
|
338
|
+
accent === "hat" || accent === "widehat"
|
|
339
|
+
? body.width === 1
|
|
340
|
+
? "^"
|
|
341
|
+
: "⌢"
|
|
342
|
+
: accent === "bar" || accent === "overline"
|
|
343
|
+
? "─"
|
|
344
|
+
: accent === "vec"
|
|
345
|
+
? "→"
|
|
346
|
+
: accent === "tilde"
|
|
347
|
+
? "~"
|
|
348
|
+
: accent === "dot"
|
|
349
|
+
? "·"
|
|
350
|
+
: "¨"
|
|
351
|
+
|
|
352
|
+
if (accent === "bar" || accent === "overline") drawHorizontal(result, 0, 0, body.width, mark, context.style)
|
|
353
|
+
else setCell(result, Math.max(0, Math.floor((body.width - cellWidth(mark)) / 2)), 0, mark, context.style)
|
|
354
|
+
return result
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function delimiterBox(
|
|
358
|
+
delimiter: string,
|
|
359
|
+
height: number,
|
|
360
|
+
baseline: number,
|
|
361
|
+
left: boolean,
|
|
362
|
+
style: MathStyle | undefined,
|
|
363
|
+
): Box {
|
|
364
|
+
if (!delimiter) return blank(0, height, baseline)
|
|
365
|
+
if (height <= 1) return textBox(delimiter, style)
|
|
366
|
+
const glyphs = delimiterGlyphs(delimiter)
|
|
367
|
+
const width = Math.max(...glyphs.map(cellWidth))
|
|
368
|
+
const result = blank(width, height, baseline)
|
|
369
|
+
for (let y = 0; y < height; y++) {
|
|
370
|
+
const glyph = y === 0 ? glyphs[0] : y === height - 1 ? glyphs[2] : glyphs[1]
|
|
371
|
+
setCell(result, 0, y, glyph, style)
|
|
372
|
+
}
|
|
373
|
+
if ((delimiter === "{" || delimiter === "}") && height >= 3) {
|
|
374
|
+
setCell(result, 0, Math.floor(height / 2), left ? "⎨" : "⎬", style)
|
|
375
|
+
}
|
|
376
|
+
return result
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function delimiterGlyphs(delimiter: string): [string, string, string] {
|
|
380
|
+
switch (delimiter) {
|
|
381
|
+
case "(":
|
|
382
|
+
return ["⎛", "⎜", "⎝"]
|
|
383
|
+
case ")":
|
|
384
|
+
return ["⎞", "⎟", "⎠"]
|
|
385
|
+
case "[":
|
|
386
|
+
return ["⎡", "⎢", "⎣"]
|
|
387
|
+
case "]":
|
|
388
|
+
return ["⎤", "⎥", "⎦"]
|
|
389
|
+
case "{":
|
|
390
|
+
return ["⎧", "⎪", "⎩"]
|
|
391
|
+
case "}":
|
|
392
|
+
return ["⎫", "⎪", "⎭"]
|
|
393
|
+
case "⌊":
|
|
394
|
+
return ["│", "│", "⌊"]
|
|
395
|
+
case "⌋":
|
|
396
|
+
return ["│", "│", "⌋"]
|
|
397
|
+
case "⌈":
|
|
398
|
+
return ["⌈", "│", "│"]
|
|
399
|
+
case "⌉":
|
|
400
|
+
return ["⌉", "│", "│"]
|
|
401
|
+
case "⟨":
|
|
402
|
+
return ["/", "│", "\\"]
|
|
403
|
+
case "⟩":
|
|
404
|
+
return ["\\", "│", "/"]
|
|
405
|
+
default:
|
|
406
|
+
return [delimiter, delimiter, delimiter]
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function matrixDelimiters(environment: string): [string, string] | undefined {
|
|
411
|
+
switch (environment) {
|
|
412
|
+
case "pmatrix":
|
|
413
|
+
return ["(", ")"]
|
|
414
|
+
case "bmatrix":
|
|
415
|
+
return ["[", "]"]
|
|
416
|
+
case "Bmatrix":
|
|
417
|
+
return ["{", "}"]
|
|
418
|
+
case "vmatrix":
|
|
419
|
+
return ["│", "│"]
|
|
420
|
+
case "Vmatrix":
|
|
421
|
+
return ["║", "║"]
|
|
422
|
+
case "cases":
|
|
423
|
+
return ["{", ""]
|
|
424
|
+
default:
|
|
425
|
+
return undefined
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function hpack(boxes: Box[]): Box {
|
|
430
|
+
if (boxes.length === 0) return blank(0, 1, 0)
|
|
431
|
+
const ascent = Math.max(...boxes.map((box) => box.baseline))
|
|
432
|
+
const descent = Math.max(...boxes.map((box) => box.height - box.baseline - 1))
|
|
433
|
+
const width = boxes.reduce((sum, box) => sum + box.width, 0)
|
|
434
|
+
const result = blank(width, ascent + descent + 1, ascent)
|
|
435
|
+
let x = 0
|
|
436
|
+
for (const box of boxes) {
|
|
437
|
+
overlay(result, box, x, ascent - box.baseline)
|
|
438
|
+
x += box.width
|
|
439
|
+
}
|
|
440
|
+
return result
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function textBox(text: string, style?: MathStyle): Box {
|
|
444
|
+
const graphemes = Array.from(graphemeSegmenter.segment(text), (item) => item.segment)
|
|
445
|
+
const width = graphemes.reduce((sum, grapheme) => sum + cellWidth(grapheme), 0)
|
|
446
|
+
const result = blank(width, 1, 0)
|
|
447
|
+
let x = 0
|
|
448
|
+
for (const grapheme of graphemes) {
|
|
449
|
+
setCell(result, x, 0, grapheme, style)
|
|
450
|
+
x += cellWidth(grapheme)
|
|
451
|
+
}
|
|
452
|
+
return result
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
function blank(width: number, height: number, baseline: number): Box {
|
|
456
|
+
return {
|
|
457
|
+
width: Math.max(0, width),
|
|
458
|
+
height: Math.max(1, height),
|
|
459
|
+
baseline: Math.max(0, baseline),
|
|
460
|
+
cells: Array.from({ length: Math.max(1, height) }, () => Array<MathCell | undefined>(Math.max(0, width))),
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function overlay(target: Box, source: Box, x: number, y: number): void {
|
|
465
|
+
for (let sourceY = 0; sourceY < source.height; sourceY++) {
|
|
466
|
+
for (let sourceX = 0; sourceX < source.width; sourceX++) {
|
|
467
|
+
const cell = source.cells[sourceY]?.[sourceX]
|
|
468
|
+
if (cell) target.cells[y + sourceY][x + sourceX] = cell
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function drawHorizontal(
|
|
474
|
+
box: Box,
|
|
475
|
+
y: number,
|
|
476
|
+
x: number,
|
|
477
|
+
width: number,
|
|
478
|
+
char: string,
|
|
479
|
+
style: MathStyle | undefined,
|
|
480
|
+
): void {
|
|
481
|
+
for (let offset = 0; offset < width; offset++) setCell(box, x + offset, y, char, style)
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function setCell(box: Box, x: number, y: number, char: string, style?: MathStyle): void {
|
|
485
|
+
if (x < 0 || y < 0 || x >= box.width || y >= box.height) return
|
|
486
|
+
box.cells[y][x] = style ? { char, style } : { char }
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
function nodeRole(node: MathNode): SymbolRole | undefined {
|
|
490
|
+
if (node.type === "symbol") return node.role
|
|
491
|
+
if (node.type === "operator") return "operator"
|
|
492
|
+
// Tall constructs need a terminal-cell side bearing. Treating them like
|
|
493
|
+
// operators gives their fraction bars/radical hooks breathing room without
|
|
494
|
+
// adding padding inside the construct itself.
|
|
495
|
+
if (node.type === "fraction" || node.type === "root" || node.type === "matrix") return "operator"
|
|
496
|
+
if (node.type === "scripts") return nodeRole(node.base)
|
|
497
|
+
return undefined
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function needsMathSpace(previous: SymbolRole | undefined, current: SymbolRole | undefined, count: number): boolean {
|
|
501
|
+
if (count === 0) return false
|
|
502
|
+
if (previous === "punctuation" || previous === "opening" || current === "punctuation" || current === "closing") {
|
|
503
|
+
return false
|
|
504
|
+
}
|
|
505
|
+
return (
|
|
506
|
+
previous === "binary" ||
|
|
507
|
+
previous === "relation" ||
|
|
508
|
+
previous === "operator" ||
|
|
509
|
+
current === "binary" ||
|
|
510
|
+
current === "relation" ||
|
|
511
|
+
current === "operator"
|
|
512
|
+
)
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
function normalizeBinaryRole(
|
|
516
|
+
role: SymbolRole | undefined,
|
|
517
|
+
previous: SymbolRole | undefined,
|
|
518
|
+
next: SymbolRole | undefined,
|
|
519
|
+
): SymbolRole | undefined {
|
|
520
|
+
if (role !== "binary") return role
|
|
521
|
+
if (
|
|
522
|
+
previous === undefined ||
|
|
523
|
+
previous === "binary" ||
|
|
524
|
+
previous === "relation" ||
|
|
525
|
+
previous === "operator" ||
|
|
526
|
+
previous === "punctuation" ||
|
|
527
|
+
previous === "opening" ||
|
|
528
|
+
next === undefined ||
|
|
529
|
+
next === "binary" ||
|
|
530
|
+
next === "relation" ||
|
|
531
|
+
next === "punctuation" ||
|
|
532
|
+
next === "closing"
|
|
533
|
+
) {
|
|
534
|
+
return "ordinary"
|
|
535
|
+
}
|
|
536
|
+
return role
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function nextSignificantRole(nodes: MathNode[], start: number): SymbolRole | undefined {
|
|
540
|
+
for (let index = start; index < nodes.length; index++) {
|
|
541
|
+
const node = nodes[index]
|
|
542
|
+
if (node.type === "space") continue
|
|
543
|
+
return nodeRole(node) ?? "ordinary"
|
|
544
|
+
}
|
|
545
|
+
return undefined
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function simpleNodeText(node: MathNode): string | undefined {
|
|
549
|
+
if (node.type === "symbol" || node.type === "text" || node.type === "operator") return node.value
|
|
550
|
+
if (node.type === "row") {
|
|
551
|
+
const values = node.body.map(simpleNodeText)
|
|
552
|
+
return values.every((value) => value !== undefined) ? values.join("") : undefined
|
|
553
|
+
}
|
|
554
|
+
return undefined
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
function mapScript(value: string | undefined, table: Readonly<Record<string, string>>): string | undefined {
|
|
558
|
+
if (value === undefined) return undefined
|
|
559
|
+
let result = ""
|
|
560
|
+
for (const char of value) {
|
|
561
|
+
const mapped = table[char]
|
|
562
|
+
if (!mapped) return undefined
|
|
563
|
+
result += mapped
|
|
564
|
+
}
|
|
565
|
+
return result
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
function withVariant(context: LayoutContext, variant: MathVariant): LayoutContext {
|
|
569
|
+
const style = variant === "bold" ? { bold: true } : variant === "italic" ? { italic: true } : {}
|
|
570
|
+
return { ...context, variant, style: { ...context.style, ...style } }
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function applyVariant(value: string, variant: MathVariant | undefined): string {
|
|
574
|
+
if (!variant || variant === "normal" || variant === "bold" || variant === "italic") return value
|
|
575
|
+
|
|
576
|
+
const exceptions: Partial<Record<MathVariant, Readonly<Record<string, string>>>> = {
|
|
577
|
+
"double-struck": {
|
|
578
|
+
C: "ℂ",
|
|
579
|
+
H: "ℍ",
|
|
580
|
+
N: "ℕ",
|
|
581
|
+
P: "ℙ",
|
|
582
|
+
Q: "ℚ",
|
|
583
|
+
R: "ℝ",
|
|
584
|
+
Z: "ℤ",
|
|
585
|
+
},
|
|
586
|
+
script: {
|
|
587
|
+
B: "ℬ",
|
|
588
|
+
E: "ℰ",
|
|
589
|
+
F: "ℱ",
|
|
590
|
+
H: "ℋ",
|
|
591
|
+
I: "ℐ",
|
|
592
|
+
L: "ℒ",
|
|
593
|
+
M: "ℳ",
|
|
594
|
+
R: "ℛ",
|
|
595
|
+
e: "ℯ",
|
|
596
|
+
g: "ℊ",
|
|
597
|
+
o: "ℴ",
|
|
598
|
+
},
|
|
599
|
+
fraktur: {
|
|
600
|
+
C: "ℭ",
|
|
601
|
+
H: "ℌ",
|
|
602
|
+
I: "ℑ",
|
|
603
|
+
R: "ℜ",
|
|
604
|
+
Z: "ℨ",
|
|
605
|
+
},
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
const ranges: Partial<Record<MathVariant, readonly [number, number, number?]>> = {
|
|
609
|
+
"double-struck": [0x1d538, 0x1d552, 0x1d7d8],
|
|
610
|
+
script: [0x1d49c, 0x1d4b6],
|
|
611
|
+
fraktur: [0x1d504, 0x1d51e],
|
|
612
|
+
sans: [0x1d5a0, 0x1d5ba, 0x1d7e2],
|
|
613
|
+
monospace: [0x1d670, 0x1d68a, 0x1d7f6],
|
|
614
|
+
}
|
|
615
|
+
const range = ranges[variant]
|
|
616
|
+
if (!range) return value
|
|
617
|
+
|
|
618
|
+
return Array.from(value)
|
|
619
|
+
.map((char) => {
|
|
620
|
+
const exception = exceptions[variant]?.[char]
|
|
621
|
+
if (exception) return exception
|
|
622
|
+
const code = char.codePointAt(0)!
|
|
623
|
+
if (code >= 65 && code <= 90) return String.fromCodePoint(range[0] + code - 65)
|
|
624
|
+
if (code >= 97 && code <= 122) return String.fromCodePoint(range[1] + code - 97)
|
|
625
|
+
if (range[2] !== undefined && code >= 48 && code <= 57) return String.fromCodePoint(range[2] + code - 48)
|
|
626
|
+
return char
|
|
627
|
+
})
|
|
628
|
+
.join("")
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
function cellWidth(value: string): number {
|
|
632
|
+
if (value.length === 0) return 0
|
|
633
|
+
if (/^(?:[\u0000-\u001f\u007f-\u009f]|[\u0300-\u036f]|[\ufe00-\ufe0f])$/u.test(value)) return 0
|
|
634
|
+
const code = value.codePointAt(0) ?? 0
|
|
635
|
+
if (
|
|
636
|
+
code >= 0x1100 &&
|
|
637
|
+
(code <= 0x115f ||
|
|
638
|
+
code === 0x2329 ||
|
|
639
|
+
code === 0x232a ||
|
|
640
|
+
(code >= 0x2e80 && code <= 0xa4cf) ||
|
|
641
|
+
(code >= 0xac00 && code <= 0xd7a3) ||
|
|
642
|
+
(code >= 0xf900 && code <= 0xfaff) ||
|
|
643
|
+
(code >= 0xfe10 && code <= 0xfe6f) ||
|
|
644
|
+
(code >= 0xff00 && code <= 0xff60) ||
|
|
645
|
+
(code >= 0xffe0 && code <= 0xffe6) ||
|
|
646
|
+
(code >= 0x1f300 && code <= 0x1faff))
|
|
647
|
+
) {
|
|
648
|
+
return 2
|
|
649
|
+
}
|
|
650
|
+
return 1
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
function asPublicLayout(box: Box): MathLayout {
|
|
654
|
+
return {
|
|
655
|
+
width: box.width,
|
|
656
|
+
height: box.height,
|
|
657
|
+
baseline: box.baseline,
|
|
658
|
+
cells: box.cells,
|
|
659
|
+
toString() {
|
|
660
|
+
return box.cells
|
|
661
|
+
.map((row) => {
|
|
662
|
+
let output = ""
|
|
663
|
+
for (let x = 0; x < box.width; x++) {
|
|
664
|
+
const cell = row[x]
|
|
665
|
+
output += cell?.char ?? " "
|
|
666
|
+
if (cell && cellWidth(cell.char) > 1) x += cellWidth(cell.char) - 1
|
|
667
|
+
}
|
|
668
|
+
return output.trimEnd()
|
|
669
|
+
})
|
|
670
|
+
.join("\n")
|
|
671
|
+
},
|
|
672
|
+
}
|
|
673
|
+
}
|