function.dart
5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
part of '../functions.dart';
EncodeResult _functionEncoder(GreenNode node) {
final functionNode = node as FunctionNode;
return NonStrictEncodeResult(
'imprecise function encoding',
'The default encoder for FunctionNode is used, which is imprecise. '
'Non better alternatives were found.',
TransparentTexEncodeResult(<dynamic>[
TexCommandEncodeResult(command: '\\operatorname', args: <dynamic>[
functionNode.functionName,
]),
functionNode.argument,
]),
);
}
final _functionOptimizationEntries = [
OptimizationEntry(
matcher: isA<FunctionNode>(
firstChild: isA<EquationRowNode>(
child: isA<StyleNode>(
matchSelf: (node) =>
node.optionsDiff.mathFontOptions ==
texMathFontOptions['\\mathrm'],
),
),
),
optimize: (node) {
final functionNode = node as FunctionNode;
texEncodingCache[node] = TransparentTexEncodeResult(<dynamic>[
TexCommandEncodeResult(command: '\\operatorname', args: <dynamic>[
_optionsDiffEncode(
(functionNode.functionName.children.first as StyleNode)
.optionsDiff
.removeMathFont(),
functionNode.functionName.children.first.children,
)
]),
functionNode.argument,
]);
},
),
// Optimization for plain invocations like \sin \lim
OptimizationEntry(
matcher: isA<FunctionNode>(
firstChild: isA<EquationRowNode>(
everyChild: isA<SymbolNode>(),
),
),
optimize: (node) {
final functionNode = node as FunctionNode;
final name =
'\\${functionNode.functionName.children.map((child) => (child as SymbolNode).symbol).join()}';
if (mathFunctions.contains(name) || mathLimits.contains(name)) {
texEncodingCache[node] = TexCommandEncodeResult(
numArgs: 1,
command: name,
args: <dynamic>[functionNode.argument],
);
}
},
),
// Optimization for non-limits-like functions with scripts
OptimizationEntry(
matcher: isA<FunctionNode>(
firstChild: isA<EquationRowNode>(
child: isA<MultiscriptsNode>(
matchSelf: (node) =>
node.presub == null &&
node.presup == null &&
isA<EquationRowNode>(
everyChild: isA<SymbolNode>(),
).match(node.base),
selfSpecificity: 500,
),
),
),
optimize: (node) {
final functionNode = node as FunctionNode;
final scriptsNode =
functionNode.functionName.children.first as MultiscriptsNode;
final name =
'\\${scriptsNode.base.children.map((child) => (child as SymbolNode).symbol).join()}';
final isFunction = mathFunctions.contains(name);
final isLimit = mathLimits.contains(name);
if (isFunction || isLimit) {
texEncodingCache[node] = TransparentTexEncodeResult(<dynamic>[
TexMultiscriptEncodeResult(
base: name + (isLimit ? '\\nolimits' : ''),
sub: scriptsNode.sub,
sup: scriptsNode.sup,
),
functionNode.argument,
]);
}
},
),
// Optimization for limits-like functions with scripts
OptimizationEntry(
matcher: isA<FunctionNode>(
firstChild: isA<EquationRowNode>(
child: isA<OverNode>(
firstChild: _nameMatcher.or(isA<EquationRowNode>(
child: isA<UnderNode>(firstChild: _nameMatcher),
)),
).or(isA<UnderNode>(
firstChild: _nameMatcher.or(isA<EquationRowNode>(
child: isA<OverNode>(firstChild: _nameMatcher),
)),
)),
),
),
optimize: (node) {
final functionNode = node as FunctionNode;
var nameNode = functionNode.functionName.children.first;
GreenNode? sub, sup;
final outer = nameNode;
if (outer is OverNode) {
sup = outer.above;
nameNode = outer.base;
// If we detect an UnderNode in the children, combined with the design
// of the matcher, we can know that there must be a inner under/over.
final inner = nameNode.children.firstOrNull;
if (inner is UnderNode) {
sub = inner.below;
nameNode = inner.base;
}
} else if (outer is UnderNode) {
sub = outer.below;
nameNode = outer.base;
final inner = nameNode.children.firstOrNull;
if (inner is OverNode) {
sup = inner.above;
nameNode = inner.base;
}
}
final name =
'\\${nameNode.children.map((child) => (child as SymbolNode).symbol).join()}';
final isFunction = mathFunctions.contains(name);
final isLimit = mathLimits.contains(name);
if (isFunction || isLimit) {
texEncodingCache[node] = TransparentTexEncodeResult(<dynamic>[
TexMultiscriptEncodeResult(
base: name + (isFunction ? '\\limits' : ''),
sub: sub,
sup: sup,
),
functionNode.argument,
]);
}
},
),
];
final _nameMatcher = isA<EquationRowNode>(
everyChild: isA<SymbolNode>(),
);