UNPKG

2.67 kBJavaScriptView Raw
1// @flow
2// Horizontal overlap functions
3import defineFunction from "../defineFunction";
4import buildCommon from "../buildCommon";
5import mathMLTree from "../mathMLTree";
6
7import * as html from "../buildHTML";
8import * as mml from "../buildMathML";
9
10defineFunction({
11 type: "lap",
12 names: ["\\mathllap", "\\mathrlap", "\\mathclap"],
13 props: {
14 numArgs: 1,
15 allowedInText: true,
16 },
17 handler: ({parser, funcName}, args) => {
18 const body = args[0];
19 return {
20 type: "lap",
21 mode: parser.mode,
22 alignment: funcName.slice(5),
23 body,
24 };
25 },
26 htmlBuilder: (group, options) => {
27 // mathllap, mathrlap, mathclap
28 let inner;
29 if (group.alignment === "clap") {
30 // ref: https://www.math.lsu.edu/~aperlis/publications/mathclap/
31 inner = buildCommon.makeSpan(
32 [], [html.buildGroup(group.body, options)]);
33 // wrap, since CSS will center a .clap > .inner > span
34 inner = buildCommon.makeSpan(["inner"], [inner], options);
35 } else {
36 inner = buildCommon.makeSpan(
37 ["inner"], [html.buildGroup(group.body, options)]);
38 }
39 const fix = buildCommon.makeSpan(["fix"], []);
40 let node = buildCommon.makeSpan(
41 [group.alignment], [inner, fix], options);
42
43 // At this point, we have correctly set horizontal alignment of the
44 // two items involved in the lap.
45 // Next, use a strut to set the height of the HTML bounding box.
46 // Otherwise, a tall argument may be misplaced.
47 const strut = buildCommon.makeSpan(["strut"]);
48 strut.style.height = (node.height + node.depth) + "em";
49 strut.style.verticalAlign = -node.depth + "em";
50 node.children.unshift(strut);
51
52 // Next, prevent vertical misplacement when next to something tall.
53 node = buildCommon.makeVList({
54 positionType: "firstBaseline",
55 children: [{type: "elem", elem: node}],
56 }, options);
57
58 // Get the horizontal spacing correct relative to adjacent items.
59 return buildCommon.makeSpan(["mord"], [node], options);
60 },
61 mathmlBuilder: (group, options) => {
62 // mathllap, mathrlap, mathclap
63 const node = new mathMLTree.MathNode(
64 "mpadded", [mml.buildGroup(group.body, options)]);
65
66 if (group.alignment !== "rlap") {
67 const offset = (group.alignment === "llap" ? "-1" : "-0.5");
68 node.setAttribute("lspace", offset + "width");
69 }
70 node.setAttribute("width", "0px");
71
72 return node;
73 },
74});