UNPKG

2.13 kBJavaScriptView Raw
1// @flow
2import defineFunction, {ordargument} from "../defineFunction";
3import buildCommon from "../buildCommon";
4
5import * as html from "../buildHTML";
6import * as mml from "../buildMathML";
7
8// Non-mathy text, possibly in a font
9const textFontFamilies = {
10 "\\text": undefined, "\\textrm": "textrm", "\\textsf": "textsf",
11 "\\texttt": "texttt", "\\textnormal": "textrm",
12};
13
14const textFontWeights = {
15 "\\textbf": "textbf",
16 "\\textmd": "textmd",
17};
18
19const textFontShapes = {
20 "\\textit": "textit",
21 "\\textup": "textup",
22};
23
24const optionsWithFont = (group, options) => {
25 const font = group.font;
26 // Checks if the argument is a font family or a font style.
27 if (!font) {
28 return options;
29 } else if (textFontFamilies[font]) {
30 return options.withTextFontFamily(textFontFamilies[font]);
31 } else if (textFontWeights[font]) {
32 return options.withTextFontWeight(textFontWeights[font]);
33 } else {
34 return options.withTextFontShape(textFontShapes[font]);
35 }
36};
37
38defineFunction({
39 type: "text",
40 names: [
41 // Font families
42 "\\text", "\\textrm", "\\textsf", "\\texttt", "\\textnormal",
43 // Font weights
44 "\\textbf", "\\textmd",
45 // Font Shapes
46 "\\textit", "\\textup",
47 ],
48 props: {
49 numArgs: 1,
50 argTypes: ["text"],
51 greediness: 2,
52 allowedInText: true,
53 consumeMode: "text",
54 },
55 handler({parser, funcName}, args) {
56 const body = args[0];
57 return {
58 type: "text",
59 mode: parser.mode,
60 body: ordargument(body),
61 font: funcName,
62 };
63 },
64 htmlBuilder(group, options) {
65 const newOptions = optionsWithFont(group, options);
66 const inner = html.buildExpression(group.body, newOptions, true);
67 return buildCommon.makeSpan(
68 ["mord", "text"], buildCommon.tryCombineChars(inner), newOptions);
69 },
70 mathmlBuilder(group, options) {
71 const newOptions = optionsWithFont(group, options);
72 return mml.buildExpressionRow(group.body, newOptions);
73 },
74});