1 |
|
2 | import defineFunction, {ordargument} from "../defineFunction";
|
3 | import buildCommon from "../buildCommon";
|
4 |
|
5 | import * as html from "../buildHTML";
|
6 | import * as mml from "../buildMathML";
|
7 |
|
8 |
|
9 | const textFontFamilies = {
|
10 | "\\text": undefined, "\\textrm": "textrm", "\\textsf": "textsf",
|
11 | "\\texttt": "texttt", "\\textnormal": "textrm",
|
12 | };
|
13 |
|
14 | const textFontWeights = {
|
15 | "\\textbf": "textbf",
|
16 | "\\textmd": "textmd",
|
17 | };
|
18 |
|
19 | const textFontShapes = {
|
20 | "\\textit": "textit",
|
21 | "\\textup": "textup",
|
22 | };
|
23 |
|
24 | const optionsWithFont = (group, options) => {
|
25 | const font = group.font;
|
26 |
|
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 |
|
38 | defineFunction({
|
39 | type: "text",
|
40 | names: [
|
41 |
|
42 | "\\text", "\\textrm", "\\textsf", "\\texttt", "\\textnormal",
|
43 |
|
44 | "\\textbf", "\\textmd",
|
45 |
|
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 | });
|