/
op.js
334 lines (307 loc) · 10.2 KB
/
op.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
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
// @flow
// Limits, symbols
import defineFunction, {ordargument} from "../defineFunction";
import buildCommon from "../buildCommon";
import {SymbolNode} from "../domTree";
import * as mathMLTree from "../mathMLTree";
import utils from "../utils";
import Style from "../Style";
import {assembleSupSub} from "./utils/assembleSupSub";
import {assertNodeType} from "../parseNode";
import {makeEm} from "../units";
import * as html from "../buildHTML";
import * as mml from "../buildMathML";
import type {HtmlBuilderSupSub, MathMLBuilder} from "../defineFunction";
import type {ParseNode} from "../parseNode";
// Most operators have a large successor symbol, but these don't.
const noSuccessor = [
"\\smallint",
];
// NOTE: Unlike most `htmlBuilder`s, this one handles not only "op", but also
// "supsub" since some of them (like \int) can affect super/subscripting.
export const htmlBuilder: HtmlBuilderSupSub<"op"> = (grp, options) => {
// Operators are handled in the TeXbook pg. 443-444, rule 13(a).
let supGroup;
let subGroup;
let hasLimits = false;
let group: ParseNode<"op">;
if (grp.type === "supsub") {
// If we have limits, supsub will pass us its group to handle. Pull
// out the superscript and subscript and set the group to the op in
// its base.
supGroup = grp.sup;
subGroup = grp.sub;
group = assertNodeType(grp.base, "op");
hasLimits = true;
} else {
group = assertNodeType(grp, "op");
}
const style = options.style;
let large = false;
if (style.size === Style.DISPLAY.size &&
group.symbol &&
!utils.contains(noSuccessor, group.name)) {
// Most symbol operators get larger in displaystyle (rule 13)
large = true;
}
let base;
if (group.symbol) {
// If this is a symbol, create the symbol.
const fontName = large ? "Size2-Regular" : "Size1-Regular";
let stash = "";
if (group.name === "\\oiint" || group.name === "\\oiiint") {
// No font glyphs yet, so use a glyph w/o the oval.
// TODO: When font glyphs are available, delete this code.
stash = group.name.slice(1);
group.name = stash === "oiint" ? "\\iint" : "\\iiint";
}
base = buildCommon.makeSymbol(
group.name, fontName, "math", options,
["mop", "op-symbol", large ? "large-op" : "small-op"]);
if (stash.length > 0) {
// We're in \oiint or \oiiint. Overlay the oval.
// TODO: When font glyphs are available, delete this code.
const italic = base.italic;
const oval = buildCommon.staticSvg(stash + "Size"
+ (large ? "2" : "1"), options);
base = buildCommon.makeVList({
positionType: "individualShift",
children: [
{type: "elem", elem: base, shift: 0},
{type: "elem", elem: oval, shift: large ? 0.08 : 0},
],
}, options);
group.name = "\\" + stash;
base.classes.unshift("mop");
// $FlowFixMe
base.italic = italic;
}
} else if (group.body) {
// If this is a list, compose that list.
const inner = html.buildExpression(group.body, options, true);
if (inner.length === 1 && inner[0] instanceof SymbolNode) {
base = inner[0];
base.classes[0] = "mop"; // replace old mclass
} else {
base = buildCommon.makeSpan(["mop"], inner, options);
}
} else {
// Otherwise, this is a text operator. Build the text from the
// operator's name.
const output = [];
for (let i = 1; i < group.name.length; i++) {
output.push(buildCommon.mathsym(group.name[i], group.mode, options));
}
base = buildCommon.makeSpan(["mop"], output, options);
}
// If content of op is a single symbol, shift it vertically.
let baseShift = 0;
let slant = 0;
if ((base instanceof SymbolNode
|| group.name === "\\oiint" || group.name === "\\oiiint")
&& !group.suppressBaseShift) {
// We suppress the shift of the base of \overset and \underset. Otherwise,
// shift the symbol so its center lies on the axis (rule 13). It
// appears that our fonts have the centers of the symbols already
// almost on the axis, so these numbers are very small. Note we
// don't actually apply this here, but instead it is used either in
// the vlist creation or separately when there are no limits.
baseShift = (base.height - base.depth) / 2 -
options.fontMetrics().axisHeight;
// The slant of the symbol is just its italic correction.
// $FlowFixMe
slant = base.italic;
}
if (hasLimits) {
return assembleSupSub(base, supGroup, subGroup, options,
style, slant, baseShift);
} else {
if (baseShift) {
base.style.position = "relative";
base.style.top = makeEm(baseShift);
}
return base;
}
};
const mathmlBuilder: MathMLBuilder<"op"> = (group, options) => {
let node;
if (group.symbol) {
// This is a symbol. Just add the symbol.
node = new mathMLTree.MathNode(
"mo", [mml.makeText(group.name, group.mode)]);
if (utils.contains(noSuccessor, group.name)) {
node.setAttribute("largeop", "false");
}
} else if (group.body) {
// This is an operator with children. Add them.
node = new mathMLTree.MathNode(
"mo", mml.buildExpression(group.body, options));
} else {
// This is a text operator. Add all of the characters from the
// operator's name.
node = new mathMLTree.MathNode(
"mi", [new mathMLTree.TextNode(group.name.slice(1))]);
// Append an <mo>⁡</mo>.
// ref: https://www.w3.org/TR/REC-MathML/chap3_2.html#sec3.2.4
const operator = new mathMLTree.MathNode("mo",
[mml.makeText("\u2061", "text")]);
if (group.parentIsSupSub) {
node = new mathMLTree.MathNode("mrow", [node, operator]);
} else {
node = mathMLTree.newDocumentFragment([node, operator]);
}
}
return node;
};
const singleCharBigOps: {[string]: string} = {
"\u220F": "\\prod",
"\u2210": "\\coprod",
"\u2211": "\\sum",
"\u22c0": "\\bigwedge",
"\u22c1": "\\bigvee",
"\u22c2": "\\bigcap",
"\u22c3": "\\bigcup",
"\u2a00": "\\bigodot",
"\u2a01": "\\bigoplus",
"\u2a02": "\\bigotimes",
"\u2a04": "\\biguplus",
"\u2a06": "\\bigsqcup",
};
defineFunction({
type: "op",
names: [
"\\coprod", "\\bigvee", "\\bigwedge", "\\biguplus", "\\bigcap",
"\\bigcup", "\\intop", "\\prod", "\\sum", "\\bigotimes",
"\\bigoplus", "\\bigodot", "\\bigsqcup", "\\smallint", "\u220F",
"\u2210", "\u2211", "\u22c0", "\u22c1", "\u22c2", "\u22c3", "\u2a00",
"\u2a01", "\u2a02", "\u2a04", "\u2a06",
],
props: {
numArgs: 0,
},
handler: ({parser, funcName}, args) => {
let fName = funcName;
if (fName.length === 1) {
fName = singleCharBigOps[fName];
}
return {
type: "op",
mode: parser.mode,
limits: true,
parentIsSupSub: false,
symbol: true,
name: fName,
};
},
htmlBuilder,
mathmlBuilder,
});
// Note: calling defineFunction with a type that's already been defined only
// works because the same htmlBuilder and mathmlBuilder are being used.
defineFunction({
type: "op",
names: ["\\mathop"],
props: {
numArgs: 1,
primitive: true,
},
handler: ({parser}, args) => {
const body = args[0];
return {
type: "op",
mode: parser.mode,
limits: false,
parentIsSupSub: false,
symbol: false,
body: ordargument(body),
};
},
htmlBuilder,
mathmlBuilder,
});
// There are 2 flags for operators; whether they produce limits in
// displaystyle, and whether they are symbols and should grow in
// displaystyle. These four groups cover the four possible choices.
const singleCharIntegrals: {[string]: string} = {
"\u222b": "\\int",
"\u222c": "\\iint",
"\u222d": "\\iiint",
"\u222e": "\\oint",
"\u222f": "\\oiint",
"\u2230": "\\oiiint",
};
// No limits, not symbols
defineFunction({
type: "op",
names: [
"\\arcsin", "\\arccos", "\\arctan", "\\arctg", "\\arcctg",
"\\arg", "\\ch", "\\cos", "\\cosec", "\\cosh", "\\cot", "\\cotg",
"\\coth", "\\csc", "\\ctg", "\\cth", "\\deg", "\\dim", "\\exp",
"\\hom", "\\ker", "\\lg", "\\ln", "\\log", "\\sec", "\\sin",
"\\sinh", "\\sh", "\\tan", "\\tanh", "\\tg", "\\th",
],
props: {
numArgs: 0,
},
handler({parser, funcName}) {
return {
type: "op",
mode: parser.mode,
limits: false,
parentIsSupSub: false,
symbol: false,
name: funcName,
};
},
htmlBuilder,
mathmlBuilder,
});
// Limits, not symbols
defineFunction({
type: "op",
names: [
"\\det", "\\gcd", "\\inf", "\\lim", "\\max", "\\min", "\\Pr", "\\sup",
],
props: {
numArgs: 0,
},
handler({parser, funcName}) {
return {
type: "op",
mode: parser.mode,
limits: true,
parentIsSupSub: false,
symbol: false,
name: funcName,
};
},
htmlBuilder,
mathmlBuilder,
});
// No limits, symbols
defineFunction({
type: "op",
names: [
"\\int", "\\iint", "\\iiint", "\\oint", "\\oiint", "\\oiiint",
"\u222b", "\u222c", "\u222d", "\u222e", "\u222f", "\u2230",
],
props: {
numArgs: 0,
},
handler({parser, funcName}) {
let fName = funcName;
if (fName.length === 1) {
fName = singleCharIntegrals[fName];
}
return {
type: "op",
mode: parser.mode,
limits: false,
parentIsSupSub: false,
symbol: true,
name: fName,
};
},
htmlBuilder,
mathmlBuilder,
});