forked from arnog/mathlive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverunder.ts
220 lines (198 loc) · 6.51 KB
/
overunder.ts
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
import { Atom } from '../core/atom-class';
import { Box, makeSVGBox } from '../core/box';
import { VBox } from '../core/v-box';
import { Context } from '../core/context';
import { makeNullDelimiter } from '../core/delimiters';
import type { AtomJson, BoxType, CreateAtomOptions } from '../core/types';
// An `overunder` atom has the following attributes:
// - body: atoms[]: atoms displayed on the base line
// - svgBody: string. A SVG graphic displayed on the base line (if present, the body is ignored)
// - above: atoms[]: atoms displayed above the body
// - svgAbove: string. A named SVG graphic above the element
// - below: atoms[]: atoms displayed below the body
// - svgBelow: string. A named SVG graphic below the element
export class OverunderAtom extends Atom {
svgAbove?: string;
svgBelow?: string;
svgBody?: string;
boxType: BoxType;
paddedBody: boolean;
paddedLabels: boolean;
constructor(
options: CreateAtomOptions & {
body?: Readonly<Atom[]>;
above?: Readonly<Atom[]>;
below?: Readonly<Atom[]>;
svgBody?: string;
svgAbove?: string;
svgBelow?: string;
skipBoundary?: boolean;
boxType?: BoxType;
supsubPlacement?: 'over-under' | 'adjacent';
paddedBody?: boolean;
paddedLabels?: boolean;
}
) {
super({
type: 'overunder',
command: options.command,
style: options.style,
mode: options.mode,
body: options.body,
skipBoundary: options.skipBoundary ?? true,
});
this.subsupPlacement = options.supsubPlacement;
this.svgAbove = options.svgAbove;
this.svgBelow = options.svgBelow;
this.svgBody = options.svgBody;
this.above = options.above;
this.below = options.below;
this.boxType = options.boxType ?? 'ord';
this.paddedBody = options.paddedBody ?? false;
this.paddedLabels = options.paddedLabels ?? false;
}
static fromJson(json: AtomJson): OverunderAtom {
return new OverunderAtom(json as any);
}
toJson(): AtomJson {
const json = super.toJson();
if (!this.skipBoundary) json.skipBoundary = false;
if (this.subsupPlacement) json.subsupPlacement = this.subsupPlacement;
if (this.svgAbove) json.svgAbove = this.svgAbove;
if (this.svgBelow) json.svgBelow = this.svgBelow;
if (this.svgBody) json.svgBody = this.svgBody;
if (this.boxType !== 'ord') json.boxType = this.boxType;
if (this.paddedBody) json.paddedBody = true;
if (this.paddedLabels) json.paddedLabels = true;
return json;
}
/**
* Combine a base with an atom above and an atom below.
*
* See http://tug.ctan.org/macros/latex/required/amsmath/amsmath.dtx
*
* > \newcommand{\overset}[2]{\binrel@{#2}%
* > \binrel@@{\mathop{\kern\z@#2}\limits^{#1}}}
*
*/
render(parentContext: Context): Box | null {
let body = this.svgBody
? makeSVGBox(this.svgBody)
: Atom.createBox(parentContext, this.body, { type: 'ignore' });
const annotationContext = new Context(
{ parent: parentContext, mathstyle: 'scriptstyle' },
this.style
);
let above: Box | null = null;
if (this.svgAbove) above = makeSVGBox(this.svgAbove);
else if (this.above)
above = Atom.createBox(annotationContext, this.above, { type: 'ignore' });
let below: Box | null = null;
// let belowShift: number;
if (this.svgBelow) below = makeSVGBox(this.svgBelow);
else if (this.below)
below = Atom.createBox(annotationContext, this.below, { type: 'ignore' });
if (this.paddedBody) {
// The base of \overset are padded, but \overbrace aren't
body = new Box(
[
makeNullDelimiter(parentContext, 'ML__open'),
body,
makeNullDelimiter(parentContext, 'ML__close'),
],
{ type: 'ignore' }
);
}
let base = makeOverunderStack(parentContext, {
base: body,
above,
below,
type:
this.boxType === 'bin' || this.boxType === 'rel' ? this.boxType : 'ord',
paddedAboveBelow: this.paddedLabels,
});
if (!base) return null;
if (this.subsupPlacement === 'over-under')
base = this.attachLimits(parentContext, { base, type: base.type });
else base = this.attachSupsub(parentContext, { base });
if (this.caret) base.caret = this.caret;
// Bind the generated box so its components can be selected
return this.bind(parentContext, base);
}
}
/**
* Combine a nucleus with an atom above and an atom below. Used to form
* stacks for the 'overunder' atom type .
*
* @param nucleus The base over and under which the atoms will
* be placed.
* @param type The type ('rel', 'bin', etc...) of the result
*/
function makeOverunderStack(
context: Context,
options: {
base: Box | null;
above: Box | null;
below: Box | null;
type: BoxType;
paddedAboveBelow: boolean;
}
): Box | null {
// If no base, nothing to do
if (!options.base) return null;
// If nothing above and nothing below, nothing to do.
if (!options.above && !options.below) {
const box = new Box(options.base, { type: options.type });
box.setStyle('position', 'relative');
return box;
}
let aboveShift = 0;
if (options.above) aboveShift = context.metrics.bigOpSpacing5; // Empirical
let result: Box | null = null;
const base = options.base;
const baseShift = 0;
// (wrappedNucleus.height - wrappedNucleus.depth) / 2 -
// context.mathstyle.metrics.axisHeight;
const classes = ['ML__center'];
if (options.paddedAboveBelow) classes.push('ML__label_padding');
if (options.below && options.above) {
const bottom =
context.metrics.bigOpSpacing5 +
options.below.height +
options.below.depth +
base.depth +
baseShift;
result = new VBox({
bottom,
children: [
context.metrics.bigOpSpacing5,
{ box: options.below, classes },
{ box: base, classes: ['ML__center'] },
aboveShift,
{ box: options.above, classes },
context.metrics.bigOpSpacing5,
],
});
} else if (options.below) {
result = new VBox({
top: base.height - baseShift,
children: [
context.metrics.bigOpSpacing5,
{ box: options.below, classes },
{ box: base, classes: ['ML__center'] },
],
});
} else if (options.above) {
result = new VBox({
bottom: base.depth + baseShift,
children: [
// base.depth,
{ box: base, classes: ['ML__center'] },
aboveShift,
{ box: options.above, classes },
context.metrics.bigOpSpacing5,
],
});
}
return new Box(result, { type: options.type });
}