UNPKG

2.92 kBJavaScriptView Raw
1// @flow
2// TODO(kevinb): implement \\sl and \\sc
3
4import {binrelClass} from "./mclass";
5import defineFunction from "../defineFunction";
6
7import * as html from "../buildHTML";
8import * as mml from "../buildMathML";
9
10import type {ParseNode} from "../parseNode";
11
12const htmlBuilder = (group: ParseNode<"font">, options) => {
13 const font = group.font;
14 const newOptions = options.withFont(font);
15 return html.buildGroup(group.body, newOptions);
16};
17
18const mathmlBuilder = (group: ParseNode<"font">, options) => {
19 const font = group.font;
20 const newOptions = options.withFont(font);
21 return mml.buildGroup(group.body, newOptions);
22};
23
24const fontAliases = {
25 "\\Bbb": "\\mathbb",
26 "\\bold": "\\mathbf",
27 "\\frak": "\\mathfrak",
28 "\\bm": "\\boldsymbol",
29};
30
31defineFunction({
32 type: "font",
33 names: [
34 // styles, except \boldsymbol defined below
35 "\\mathrm", "\\mathit", "\\mathbf", "\\mathnormal",
36
37 // families
38 "\\mathbb", "\\mathcal", "\\mathfrak", "\\mathscr", "\\mathsf",
39 "\\mathtt",
40
41 // aliases, except \bm defined below
42 "\\Bbb", "\\bold", "\\frak",
43 ],
44 props: {
45 numArgs: 1,
46 greediness: 2,
47 },
48 handler: ({parser, funcName}, args) => {
49 const body = args[0];
50 let func = funcName;
51 if (func in fontAliases) {
52 func = fontAliases[func];
53 }
54 return {
55 type: "font",
56 mode: parser.mode,
57 font: func.slice(1),
58 body,
59 };
60 },
61 htmlBuilder,
62 mathmlBuilder,
63});
64
65defineFunction({
66 type: "mclass",
67 names: ["\\boldsymbol", "\\bm"],
68 props: {
69 numArgs: 1,
70 greediness: 2,
71 },
72 handler: ({parser}, args) => {
73 const body = args[0];
74 // amsbsy.sty's \boldsymbol uses \binrel spacing to inherit the
75 // argument's bin|rel|ord status
76 return {
77 type: "mclass",
78 mode: parser.mode,
79 mclass: binrelClass(body),
80 body: [
81 {
82 type: "font",
83 mode: parser.mode,
84 font: "boldsymbol",
85 body,
86 },
87 ],
88 };
89 },
90});
91
92// Old font changing functions
93defineFunction({
94 type: "font",
95 names: ["\\rm", "\\sf", "\\tt", "\\bf", "\\it"],
96 props: {
97 numArgs: 0,
98 allowedInText: true,
99 },
100 handler: ({parser, funcName, breakOnTokenText}, args) => {
101 const {mode} = parser;
102 const body = parser.parseExpression(true, breakOnTokenText);
103 const style = `math${funcName.slice(1)}`;
104
105 return {
106 type: "font",
107 mode: mode,
108 font: style,
109 body: {
110 type: "ordgroup",
111 mode: parser.mode,
112 body,
113 },
114 };
115 },
116 htmlBuilder,
117 mathmlBuilder,
118});