1 |
|
2 | import buildCommon from "../buildCommon";
|
3 | import defineFunction from "../defineFunction";
|
4 | import mathMLTree from "../mathMLTree";
|
5 |
|
6 | import * as html from "../buildHTML";
|
7 | import * as mml from "../buildMathML";
|
8 |
|
9 | import type Options from "../Options";
|
10 | import type {AnyParseNode} from "../parseNode";
|
11 | import type {HtmlBuilder} from "../defineFunction";
|
12 | import type {documentFragment as HtmlDocumentFragment} from "../domTree";
|
13 |
|
14 | export function sizingGroup(
|
15 | value: AnyParseNode[],
|
16 | options: Options,
|
17 | baseOptions: Options,
|
18 | ): HtmlDocumentFragment {
|
19 | const inner = html.buildExpression(value, options, false);
|
20 | const multiplier = options.sizeMultiplier / baseOptions.sizeMultiplier;
|
21 |
|
22 |
|
23 |
|
24 | for (let i = 0; i < inner.length; i++) {
|
25 | const pos = inner[i].classes.indexOf("sizing");
|
26 | if (pos < 0) {
|
27 | Array.prototype.push.apply(inner[i].classes,
|
28 | options.sizingClasses(baseOptions));
|
29 | } else if (inner[i].classes[pos + 1] === "reset-size" + options.size) {
|
30 |
|
31 |
|
32 |
|
33 | inner[i].classes[pos + 1] = "reset-size" + baseOptions.size;
|
34 | }
|
35 |
|
36 | inner[i].height *= multiplier;
|
37 | inner[i].depth *= multiplier;
|
38 | }
|
39 |
|
40 | return buildCommon.makeFragment(inner);
|
41 | }
|
42 |
|
43 | const sizeFuncs = [
|
44 | "\\tiny", "\\sixptsize", "\\scriptsize", "\\footnotesize", "\\small",
|
45 | "\\normalsize", "\\large", "\\Large", "\\LARGE", "\\huge", "\\Huge",
|
46 | ];
|
47 |
|
48 | export const htmlBuilder: HtmlBuilder<"sizing"> = (group, options) => {
|
49 |
|
50 |
|
51 |
|
52 | const newOptions = options.havingSize(group.size);
|
53 | return sizingGroup(group.body, newOptions, options);
|
54 | };
|
55 |
|
56 | defineFunction({
|
57 | type: "sizing",
|
58 | names: sizeFuncs,
|
59 | props: {
|
60 | numArgs: 0,
|
61 | allowedInText: true,
|
62 | },
|
63 | handler: ({breakOnTokenText, funcName, parser}, args) => {
|
64 | const body = parser.parseExpression(false, breakOnTokenText);
|
65 |
|
66 | return {
|
67 | type: "sizing",
|
68 | mode: parser.mode,
|
69 |
|
70 | size: sizeFuncs.indexOf(funcName) + 1,
|
71 | body,
|
72 | };
|
73 | },
|
74 | htmlBuilder,
|
75 | mathmlBuilder: (group, options) => {
|
76 | const newOptions = options.havingSize(group.size);
|
77 | const inner = mml.buildExpression(group.body, newOptions);
|
78 |
|
79 | const node = new mathMLTree.MathNode("mstyle", inner);
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | node.setAttribute("mathsize", newOptions.sizeMultiplier + "em");
|
87 |
|
88 | return node;
|
89 | },
|
90 | });
|