forked from KaTeX/KaTeX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
supsub.js
248 lines (218 loc) · 8.59 KB
/
supsub.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
// @flow
import {defineFunctionBuilders} from "../defineFunction";
import buildCommon from "../buildCommon";
import {SymbolNode} from "../domTree";
import mathMLTree from "../mathMLTree";
import utils from "../utils";
import Style from "../Style";
import {checkNodeType} from "../parseNode";
import * as html from "../buildHTML";
import * as mml from "../buildMathML";
import * as accent from "./accent";
import * as horizBrace from "./horizBrace";
import * as op from "./op";
import type Options from "../Options";
import type {ParseNode} from "../parseNode";
import type {HtmlBuilder} from "../defineFunction";
import type {MathNodeType} from "../mathMLTree";
/**
* Sometimes, groups perform special rules when they have superscripts or
* subscripts attached to them. This function lets the `supsub` group know that
* Sometimes, groups perform special rules when they have superscripts or
* its inner element should handle the superscripts and subscripts instead of
* handling them itself.
*/
const htmlBuilderDelegate = function(
group: ParseNode<"supsub">,
options: Options,
): ?HtmlBuilder<*> {
const base = group.base;
if (!base) {
return null;
} else if (base.type === "op") {
// Operators handle supsubs differently when they have limits
// (e.g. `\displaystyle\sum_2^3`)
const delegate = base.limits &&
(options.style.size === Style.DISPLAY.size ||
base.alwaysHandleSupSub);
return delegate ? op.htmlBuilder : null;
} else if (base.type === "accent") {
return utils.isCharacterBox(base.base) ? accent.htmlBuilder : null;
} else if (base.type === "horizBrace") {
const isSup = !group.sub;
return isSup === base.isOver ? horizBrace.htmlBuilder : null;
} else {
return null;
}
};
// Super scripts and subscripts, whose precise placement can depend on other
// functions that precede them.
defineFunctionBuilders({
type: "supsub",
htmlBuilder(group, options) {
// Superscript and subscripts are handled in the TeXbook on page
// 445-446, rules 18(a-f).
// Here is where we defer to the inner group if it should handle
// superscripts and subscripts itself.
const builderDelegate = htmlBuilderDelegate(group, options);
if (builderDelegate) {
return builderDelegate(group, options);
}
const {base: valueBase, sup: valueSup, sub: valueSub} = group;
const base = html.buildGroup(valueBase, options);
let supm;
let subm;
const metrics = options.fontMetrics();
// Rule 18a
let supShift = 0;
let subShift = 0;
const isCharacterBox = valueBase && utils.isCharacterBox(valueBase);
if (valueSup) {
const newOptions = options.havingStyle(options.style.sup());
supm = html.buildGroup(valueSup, newOptions, options);
if (!isCharacterBox) {
supShift = base.height - newOptions.fontMetrics().supDrop
* newOptions.sizeMultiplier / options.sizeMultiplier;
}
}
if (valueSub) {
const newOptions = options.havingStyle(options.style.sub());
subm = html.buildGroup(valueSub, newOptions, options);
if (!isCharacterBox) {
subShift = base.depth + newOptions.fontMetrics().subDrop
* newOptions.sizeMultiplier / options.sizeMultiplier;
}
}
// Rule 18c
let minSupShift;
if (options.style === Style.DISPLAY) {
minSupShift = metrics.sup1;
} else if (options.style.cramped) {
minSupShift = metrics.sup3;
} else {
minSupShift = metrics.sup2;
}
// scriptspace is a font-size-independent size, so scale it
// appropriately for use as the marginRight.
const multiplier = options.sizeMultiplier;
const marginRight = (0.5 / metrics.ptPerEm) / multiplier + "em";
let marginLeft = null;
if (subm) {
// Subscripts shouldn't be shifted by the base's italic correction.
// Account for that by shifting the subscript back the appropriate
// amount. Note we only do this when the base is a single symbol.
const isOiint =
group.base && group.base.type === "op" && group.base.name &&
(group.base.name === "\\oiint" || group.base.name === "\\oiiint");
if (base instanceof SymbolNode || isOiint) {
// $FlowFixMe
marginLeft = -base.italic + "em";
}
}
let supsub;
if (supm && subm) {
supShift = Math.max(
supShift, minSupShift, supm.depth + 0.25 * metrics.xHeight);
subShift = Math.max(subShift, metrics.sub2);
const ruleWidth = metrics.defaultRuleThickness;
// Rule 18e
const maxWidth = 4 * ruleWidth;
if ((supShift - supm.depth) - (subm.height - subShift) < maxWidth) {
subShift = maxWidth - (supShift - supm.depth) + subm.height;
const psi = 0.8 * metrics.xHeight - (supShift - supm.depth);
if (psi > 0) {
supShift += psi;
subShift -= psi;
}
}
const vlistElem = [
{type: "elem", elem: subm, shift: subShift, marginRight,
marginLeft},
{type: "elem", elem: supm, shift: -supShift, marginRight},
];
supsub = buildCommon.makeVList({
positionType: "individualShift",
children: vlistElem,
}, options);
} else if (subm) {
// Rule 18b
subShift = Math.max(
subShift, metrics.sub1,
subm.height - 0.8 * metrics.xHeight);
const vlistElem =
[{type: "elem", elem: subm, marginLeft, marginRight}];
supsub = buildCommon.makeVList({
positionType: "shift",
positionData: subShift,
children: vlistElem,
}, options);
} else if (supm) {
// Rule 18c, d
supShift = Math.max(supShift, minSupShift,
supm.depth + 0.25 * metrics.xHeight);
supsub = buildCommon.makeVList({
positionType: "shift",
positionData: -supShift,
children: [{type: "elem", elem: supm, marginRight}],
}, options);
} else {
throw new Error("supsub must have either sup or sub.");
}
// Wrap the supsub vlist in a span.msupsub to reset text-align.
const mclass = html.getTypeOfDomTree(base, "right") || "mord";
return buildCommon.makeSpan([mclass],
[base, buildCommon.makeSpan(["msupsub"], [supsub])],
options);
},
mathmlBuilder(group, options) {
// Is the inner group a relevant horizonal brace?
let isBrace = false;
let isOver;
let isSup;
const horizBrace = checkNodeType(group.base, "horizBrace");
if (horizBrace) {
isSup = !!group.sup;
if (isSup === horizBrace.isOver) {
isBrace = true;
isOver = horizBrace.isOver;
}
}
const children = [mml.buildGroup(group.base, options)];
if (group.sub) {
children.push(mml.buildGroup(group.sub, options));
}
if (group.sup) {
children.push(mml.buildGroup(group.sup, options));
}
let nodeType: MathNodeType;
if (isBrace) {
nodeType = (isOver ? "mover" : "munder");
} else if (!group.sub) {
const base = group.base;
if (base && base.type === "op" && base.limits &&
options.style === Style.DISPLAY) {
nodeType = "mover";
} else {
nodeType = "msup";
}
} else if (!group.sup) {
const base = group.base;
if (base && base.type === "op" && base.limits &&
options.style === Style.DISPLAY) {
nodeType = "munder";
} else {
nodeType = "msub";
}
} else {
const base = group.base;
if (base && base.type === "op" && base.limits &&
options.style === Style.DISPLAY) {
nodeType = "munderover";
} else {
nodeType = "msubsup";
}
}
const node = new mathMLTree.MathNode(nodeType, children);
return node;
},
});