forked from KaTeX/KaTeX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelimsizing.js
301 lines (267 loc) · 10 KB
/
delimsizing.js
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
// @flow
import buildCommon from "../buildCommon";
import defineFunction from "../defineFunction";
import delimiter from "../delimiter";
import mathMLTree from "../mathMLTree";
import ParseError from "../ParseError";
import utils from "../utils";
import { calculateSize } from "../units";
import { spacings, tightSpacings } from "../spacingData";
import * as html from "../buildHTML";
import * as mml from "../buildMathML";
import type ParseNode from "../ParseNode";
import type {FunctionContext} from "../defineFunction";
// Extra data needed for the delimiter handler down below
const delimiterSizes = {
"\\bigl" : {mclass: "mopen", size: 1},
"\\Bigl" : {mclass: "mopen", size: 2},
"\\biggl": {mclass: "mopen", size: 3},
"\\Biggl": {mclass: "mopen", size: 4},
"\\bigr" : {mclass: "mclose", size: 1},
"\\Bigr" : {mclass: "mclose", size: 2},
"\\biggr": {mclass: "mclose", size: 3},
"\\Biggr": {mclass: "mclose", size: 4},
"\\bigm" : {mclass: "mrel", size: 1},
"\\Bigm" : {mclass: "mrel", size: 2},
"\\biggm": {mclass: "mrel", size: 3},
"\\Biggm": {mclass: "mrel", size: 4},
"\\big" : {mclass: "mord", size: 1},
"\\Big" : {mclass: "mord", size: 2},
"\\bigg" : {mclass: "mord", size: 3},
"\\Bigg" : {mclass: "mord", size: 4},
};
const delimiters = [
"(", ")", "[", "\\lbrack", "]", "\\rbrack",
"\\{", "\\lbrace", "\\}", "\\rbrace",
"\\lfloor", "\\rfloor", "\\lceil", "\\rceil",
"<", ">", "\\langle", "\\rangle", "\\lt", "\\gt",
"\\lvert", "\\rvert", "\\lVert", "\\rVert",
"\\lgroup", "\\rgroup", "\\lmoustache", "\\rmoustache",
"/", "\\backslash",
"|", "\\vert", "\\|", "\\Vert",
"\\uparrow", "\\Uparrow",
"\\downarrow", "\\Downarrow",
"\\updownarrow", "\\Updownarrow",
".",
];
// Delimiter functions
function checkDelimiter(delim: ParseNode, context: FunctionContext): ParseNode {
if (utils.contains(delimiters, delim.value)) {
return delim;
} else {
throw new ParseError(
"Invalid delimiter: '" + delim.value + "' after '" +
context.funcName + "'", delim);
}
}
defineFunction({
type: "delimsizing",
names: [
"\\bigl", "\\Bigl", "\\biggl", "\\Biggl",
"\\bigr", "\\Bigr", "\\biggr", "\\Biggr",
"\\bigm", "\\Bigm", "\\biggm", "\\Biggm",
"\\big", "\\Big", "\\bigg", "\\Bigg",
],
props: {
numArgs: 1,
},
handler: (context, args) => {
const delim = checkDelimiter(args[0], context);
return {
type: "delimsizing",
size: delimiterSizes[context.funcName].size,
mclass: delimiterSizes[context.funcName].mclass,
value: delim.value,
};
},
htmlBuilder: (group, options) => {
const delim = group.value.value;
if (delim === ".") {
// Empty delimiters still count as elements, even though they don't
// show anything.
return buildCommon.makeSpan([group.value.mclass]);
}
// Use delimiter.sizedDelim to generate the delimiter.
return delimiter.sizedDelim(
delim, group.value.size, options, group.mode,
[group.value.mclass]);
},
mathmlBuilder: (group) => {
const children = [];
if (group.value.value !== ".") {
children.push(mml.makeText(group.value.value, group.mode));
}
const node = new mathMLTree.MathNode("mo", children);
if (group.value.mclass === "mopen" ||
group.value.mclass === "mclose") {
// Only some of the delimsizing functions act as fences, and they
// return "mopen" or "mclose" mclass.
node.setAttribute("fence", "true");
} else {
// Explicitly disable fencing if it's not a fence, to override the
// defaults.
node.setAttribute("fence", "false");
}
return node;
},
});
defineFunction({
type: "leftright",
names: [
"\\left", "\\right",
],
props: {
numArgs: 1,
},
handler: (context, args) => {
const delim = checkDelimiter(args[0], context);
if (context.funcName === "\\left") {
const parser = context.parser;
// Parse out the implicit body
++parser.leftrightDepth;
// parseExpression stops before '\\right'
const body = parser.parseExpression(false);
--parser.leftrightDepth;
// Check the next token
parser.expect("\\right", false);
const right = parser.parseFunction();
if (!right) {
throw new ParseError('failed to parse function after \\right');
}
return {
type: "leftright",
body: body,
left: delim.value,
right: right.value.value,
};
} else {
// This is a little weird. We return this object which gets turned
// into a ParseNode which gets returned by
// `const right = parser.parseFunction();` up above.
return {
type: "leftright",
value: delim.value,
};
}
},
htmlBuilder: (group, options) => {
// Build the inner expression
const inner = html.buildExpression(group.value.body, options, true);
let innerHeight = 0;
let innerDepth = 0;
let hadMiddle = false;
// Calculate its height and depth
for (let i = 0; i < inner.length; i++) {
if (inner[i].isMiddle) {
hadMiddle = true;
} else {
innerHeight = Math.max(inner[i].height, innerHeight);
innerDepth = Math.max(inner[i].depth, innerDepth);
}
}
// The size of delimiters is the same, regardless of what style we are
// in. Thus, to correctly calculate the size of delimiter we need around
// a group, we scale down the inner size based on the size.
innerHeight *= options.sizeMultiplier;
innerDepth *= options.sizeMultiplier;
let leftDelim;
if (group.value.left === ".") {
// Empty delimiters in \left and \right make null delimiter spaces.
leftDelim = html.makeNullDelimiter(options, ["mopen"]);
} else {
// Otherwise, use leftRightDelim to generate the correct sized
// delimiter.
leftDelim = delimiter.leftRightDelim(
group.value.left, innerHeight, innerDepth, options,
group.mode, ["mopen"]);
}
// Add it to the beginning of the expression
inner.unshift(leftDelim);
// Handle middle delimiters
if (hadMiddle) {
for (let i = 1; i < inner.length; i++) {
const middleDelim = inner[i];
if (middleDelim.isMiddle) {
// Apply the options that were active when \middle was called
inner[i] = delimiter.leftRightDelim(
middleDelim.isMiddle.value, innerHeight, innerDepth,
middleDelim.isMiddle.options, group.mode, []);
}
}
}
const lastChildType = html.getTypeOfDomTree(inner[inner.length - 1]);
const activeSpacings = options.style.isTight() ? tightSpacings : spacings;
if (lastChildType && activeSpacings[lastChildType]["mclose"]) {
const glue =
buildCommon.makeSpan(["mord", "rule"], [], options);
const dimension =
calculateSize(activeSpacings[lastChildType]["mclose"], options);
glue.style.marginRight = `${dimension}em`;
inner.push(glue);
}
let rightDelim;
// Same for the right delimiter
if (group.value.right === ".") {
rightDelim = html.makeNullDelimiter(options, ["mclose"]);
} else {
rightDelim = delimiter.leftRightDelim(
group.value.right, innerHeight, innerDepth, options,
group.mode, ["mclose"]);
}
// Add it to the end of the expression.
inner.push(rightDelim);
return buildCommon.makeSpan(["minner"], inner, options);
},
mathmlBuilder: (group, options) => {
const inner = mml.buildExpression(group.value.body, options);
if (group.value.left !== ".") {
const leftNode = new mathMLTree.MathNode(
"mo", [mml.makeText(group.value.left, group.mode)]);
leftNode.setAttribute("fence", "true");
inner.unshift(leftNode);
}
if (group.value.right !== ".") {
const rightNode = new mathMLTree.MathNode(
"mo", [mml.makeText(group.value.right, group.mode)]);
rightNode.setAttribute("fence", "true");
inner.push(rightNode);
}
const outerNode = new mathMLTree.MathNode("mrow", inner);
return outerNode;
},
});
defineFunction({
type: "middle",
names: ["\\middle"],
props: {
numArgs: 1,
},
handler: (context, args) => {
const delim = checkDelimiter(args[0], context);
if (!context.parser.leftrightDepth) {
throw new ParseError("\\middle without preceding \\left", delim);
}
return {
type: "middle",
value: delim.value,
};
},
htmlBuilder: (group, options) => {
let middleDelim;
if (group.value.value === ".") {
middleDelim = html.makeNullDelimiter(options, []);
} else {
middleDelim = delimiter.sizedDelim(
group.value.value, 1, options,
group.mode, []);
middleDelim.isMiddle = {value: group.value.value, options: options};
}
return middleDelim;
},
mathmlBuilder: (group, options) => {
const middleNode = new mathMLTree.MathNode(
"mo", [mml.makeText(group.value.middle, group.mode)]);
middleNode.setAttribute("fence", "true");
return middleNode;
},
});