UNPKG

2.77 kBJavaScriptView Raw
1// @flow
2import buildCommon from "../buildCommon";
3import defineFunction from "../defineFunction";
4import mathMLTree from "../mathMLTree";
5import {assertNodeType} from "../parseNode";
6import {calculateSize} from "../units";
7
8defineFunction({
9 type: "rule",
10 names: ["\\rule"],
11 props: {
12 numArgs: 2,
13 numOptionalArgs: 1,
14 argTypes: ["size", "size", "size"],
15 },
16 handler({parser}, args, optArgs) {
17 const shift = optArgs[0];
18 const width = assertNodeType(args[0], "size");
19 const height = assertNodeType(args[1], "size");
20 return {
21 type: "rule",
22 mode: parser.mode,
23 shift: shift && assertNodeType(shift, "size").value,
24 width: width.value,
25 height: height.value,
26 };
27 },
28 htmlBuilder(group, options) {
29 // Make an empty span for the rule
30 const rule = buildCommon.makeSpan(["mord", "rule"], [], options);
31
32 // Calculate the shift, width, and height of the rule, and account for units
33 const width = calculateSize(group.width, options);
34 const height = calculateSize(group.height, options);
35 const shift = (group.shift) ? calculateSize(group.shift, options) : 0;
36
37 // Style the rule to the right size
38 rule.style.borderRightWidth = width + "em";
39 rule.style.borderTopWidth = height + "em";
40 rule.style.bottom = shift + "em";
41
42 // Record the height and width
43 rule.width = width;
44 rule.height = height + shift;
45 rule.depth = -shift;
46 // Font size is the number large enough that the browser will
47 // reserve at least `absHeight` space above the baseline.
48 // The 1.125 factor was empirically determined
49 rule.maxFontSize = height * 1.125 * options.sizeMultiplier;
50
51 return rule;
52 },
53 mathmlBuilder(group, options) {
54 const width = calculateSize(group.width, options);
55 const height = calculateSize(group.height, options);
56 const shift = (group.shift) ? calculateSize(group.shift, options) : 0;
57 const color = options.color && options.getColor() || "black";
58
59 const rule = new mathMLTree.MathNode("mspace");
60 rule.setAttribute("mathbackground", color);
61 rule.setAttribute("width", width + "em");
62 rule.setAttribute("height", height + "em");
63
64 const wrapper = new mathMLTree.MathNode("mpadded", [rule]);
65 if (shift >= 0) {
66 wrapper.setAttribute("height", "+" + shift + "em");
67 } else {
68 wrapper.setAttribute("height", shift + "em");
69 wrapper.setAttribute("depth", "+" + (-shift) + "em");
70 }
71 wrapper.setAttribute("voffset", shift + "em");
72
73 return wrapper;
74 },
75});