UNPKG

2.61 kBJavaScriptView Raw
1// @flow
2import defineFunction from "../defineFunction";
3import mathMLTree from "../mathMLTree";
4import Style from "../Style";
5import {sizingGroup} from "./sizing";
6
7import * as mml from "../buildMathML";
8
9const styleMap = {
10 "display": Style.DISPLAY,
11 "text": Style.TEXT,
12 "script": Style.SCRIPT,
13 "scriptscript": Style.SCRIPTSCRIPT,
14};
15
16defineFunction({
17 type: "styling",
18 names: [
19 "\\displaystyle", "\\textstyle", "\\scriptstyle",
20 "\\scriptscriptstyle",
21 ],
22 props: {
23 numArgs: 0,
24 allowedInText: true,
25 },
26 handler({breakOnTokenText, funcName, parser}, args) {
27 // parse out the implicit body
28 const body = parser.parseExpression(true, breakOnTokenText);
29
30 // TODO: Refactor to avoid duplicating styleMap in multiple places (e.g.
31 // here and in buildHTML and de-dupe the enumeration of all the styles).
32 // $FlowFixMe: The names above exactly match the styles.
33 const style: StyleStr = funcName.slice(1, funcName.length - 5);
34 return {
35 type: "styling",
36 mode: parser.mode,
37 // Figure out what style to use by pulling out the style from
38 // the function name
39 style,
40 body,
41 };
42 },
43 htmlBuilder(group, options) {
44 // Style changes are handled in the TeXbook on pg. 442, Rule 3.
45 const newStyle = styleMap[group.style];
46 const newOptions = options.havingStyle(newStyle).withFont('');
47 return sizingGroup(group.body, newOptions, options);
48 },
49 mathmlBuilder(group, options) {
50 // Figure out what style we're changing to.
51 // TODO(kevinb): dedupe this with buildHTML.js
52 // This will be easier of handling of styling nodes is in the same file.
53 const styleMap = {
54 "display": Style.DISPLAY,
55 "text": Style.TEXT,
56 "script": Style.SCRIPT,
57 "scriptscript": Style.SCRIPTSCRIPT,
58 };
59
60 const newStyle = styleMap[group.style];
61 const newOptions = options.havingStyle(newStyle);
62
63 const inner = mml.buildExpression(group.body, newOptions);
64
65 const node = new mathMLTree.MathNode("mstyle", inner);
66
67 const styleAttributes = {
68 "display": ["0", "true"],
69 "text": ["0", "false"],
70 "script": ["1", "false"],
71 "scriptscript": ["2", "false"],
72 };
73
74 const attr = styleAttributes[group.style];
75
76 node.setAttribute("scriptlevel", attr[0]);
77 node.setAttribute("displaystyle", attr[1]);
78
79 return node;
80 },
81});