| import defineFunction from "../defineFunction"; |
| import {MathNode} from "../mathMLTree"; |
| import Style from "../Style"; |
| import {sizingGroup} from "./sizing"; |
|
|
| import * as mml from "../buildMathML"; |
| import type {StyleStr} from "../types"; |
|
|
| const styleMap: Record<StyleStr, typeof Style.DISPLAY> = { |
| "display": Style.DISPLAY, |
| "text": Style.TEXT, |
| "script": Style.SCRIPT, |
| "scriptscript": Style.SCRIPTSCRIPT, |
| }; |
|
|
| function isStyleStr(s: string): s is StyleStr { |
| return s in styleMap; |
| } |
|
|
| defineFunction({ |
| type: "styling", |
| names: [ |
| "\\displaystyle", "\\textstyle", "\\scriptstyle", |
| "\\scriptscriptstyle", |
| ], |
| numArgs: 0, |
| allowedInText: true, |
| primitive: true, |
|
|
| handler({breakOnTokenText, funcName, parser}, args) { |
| |
| const body = parser.parseExpression(true, breakOnTokenText); |
|
|
| |
| |
| const style = funcName.slice(1, funcName.length - 5); |
| if (!isStyleStr(style)) { |
| throw new Error(`Unknown style: ${style}`); |
| } |
| return { |
| type: "styling", |
| mode: parser.mode, |
| |
| |
| style, |
| body, |
| }; |
| }, |
|
|
| htmlBuilder(group, options) { |
| |
| const newStyle = styleMap[group.style]; |
| let newOptions = options.havingStyle(newStyle); |
| if (group.resetFont) { |
| newOptions = newOptions.withFont(''); |
| } |
| return sizingGroup(group.body, newOptions, options); |
| }, |
| mathmlBuilder(group, options) { |
| |
| const newStyle = styleMap[group.style]; |
| let newOptions = options.havingStyle(newStyle); |
| if (group.resetFont) { |
| newOptions = newOptions.withFont(''); |
| } |
|
|
| const inner = mml.buildExpression(group.body, newOptions); |
|
|
| const node = new MathNode("mstyle", inner); |
|
|
| const styleAttributes = { |
| "display": ["0", "true"], |
| "text": ["0", "false"], |
| "script": ["1", "false"], |
| "scriptscript": ["2", "false"], |
| }; |
|
|
| const attr = styleAttributes[group.style]; |
|
|
| node.setAttribute("scriptlevel", attr[0]); |
| node.setAttribute("displaystyle", attr[1]); |
|
|
| return node; |
| }, |
| }); |
|
|