forked from KaTeX/KaTeX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule.js
75 lines (67 loc) · 2.71 KB
/
rule.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
// @flow
import buildCommon from "../buildCommon";
import defineFunction from "../defineFunction";
import mathMLTree from "../mathMLTree";
import {assertNodeType} from "../parseNode";
import {calculateSize, makeEm} from "../units";
defineFunction({
type: "rule",
names: ["\\rule"],
props: {
numArgs: 2,
numOptionalArgs: 1,
argTypes: ["size", "size", "size"],
},
handler({parser}, args, optArgs) {
const shift = optArgs[0];
const width = assertNodeType(args[0], "size");
const height = assertNodeType(args[1], "size");
return {
type: "rule",
mode: parser.mode,
shift: shift && assertNodeType(shift, "size").value,
width: width.value,
height: height.value,
};
},
htmlBuilder(group, options) {
// Make an empty span for the rule
const rule = buildCommon.makeSpan(["mord", "rule"], [], options);
// Calculate the shift, width, and height of the rule, and account for units
const width = calculateSize(group.width, options);
const height = calculateSize(group.height, options);
const shift = (group.shift) ? calculateSize(group.shift, options) : 0;
// Style the rule to the right size
rule.style.borderRightWidth = makeEm(width);
rule.style.borderTopWidth = makeEm(height);
rule.style.bottom = makeEm(shift);
// Record the height and width
rule.width = width;
rule.height = height + shift;
rule.depth = -shift;
// Font size is the number large enough that the browser will
// reserve at least `absHeight` space above the baseline.
// The 1.125 factor was empirically determined
rule.maxFontSize = height * 1.125 * options.sizeMultiplier;
return rule;
},
mathmlBuilder(group, options) {
const width = calculateSize(group.width, options);
const height = calculateSize(group.height, options);
const shift = (group.shift) ? calculateSize(group.shift, options) : 0;
const color = options.color && options.getColor() || "black";
const rule = new mathMLTree.MathNode("mspace");
rule.setAttribute("mathbackground", color);
rule.setAttribute("width", makeEm(width));
rule.setAttribute("height", makeEm(height));
const wrapper = new mathMLTree.MathNode("mpadded", [rule]);
if (shift >= 0) {
wrapper.setAttribute("height", makeEm(shift));
} else {
wrapper.setAttribute("height", makeEm(shift));
wrapper.setAttribute("depth", makeEm(-shift));
}
wrapper.setAttribute("voffset", makeEm(shift));
return wrapper;
},
});