frac.dart
5.93 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import '../../render/layout/custom_layout.dart';
import '../options.dart';
import '../size.dart';
import '../style.dart';
import '../syntax_tree.dart';
/// Frac node.
class FracNode extends SlotableNode<EquationRowNode> {
/// Numerator.
final EquationRowNode numerator;
/// Denumerator.
final EquationRowNode denominator;
/// Bar size.
///
/// If null, will use default bar size.
final Measurement? barSize;
/// Whether it is a continued frac `\cfrac`.
final bool continued; // TODO continued
FracNode({
// this.options,
required this.numerator,
required this.denominator,
this.barSize,
this.continued = false,
});
@override
List<EquationRowNode> computeChildren() => [numerator, denominator];
@override
BuildResult buildWidget(
MathOptions options, List<BuildResult?> childBuildResults) =>
BuildResult(
options: options,
widget: CustomLayout(
delegate: FracLayoutDelegate(
barSize: barSize,
options: options,
),
children: <Widget>[
CustomLayoutId(
id: _FracPos.numer,
child: childBuildResults[0]!.widget,
),
CustomLayoutId(
id: _FracPos.denom,
child: childBuildResults[1]!.widget,
),
],
),
);
@override
List<MathOptions> computeChildOptions(MathOptions options) => [
options.havingStyle(options.style.fracNum()),
options.havingStyle(options.style.fracDen()),
];
@override
bool shouldRebuildWidget(MathOptions oldOptions, MathOptions newOptions) =>
false;
@override
FracNode updateChildren(List<EquationRowNode> newChildren) => FracNode(
// options: options ?? this.options,
numerator: newChildren[0],
denominator: newChildren[1],
barSize: barSize,
);
@override
AtomType get leftType => AtomType.ord;
@override
AtomType get rightType => AtomType.ord;
@override
Map<String, Object?> toJson() => super.toJson()
..addAll({
'numerator': numerator.toJson(),
'denominator': denominator.toJson(),
if (barSize != null) 'barSize': barSize.toString(),
if (continued) 'continued': continued,
});
}
enum _FracPos {
numer,
denom,
}
class FracLayoutDelegate extends IntrinsicLayoutDelegate<_FracPos> {
final Measurement? barSize;
final MathOptions options;
FracLayoutDelegate({
required this.barSize,
required this.options,
});
var theta = 0.0;
var height = 0.0;
var a = 0.0;
var width = 0.0;
var barLength = 0.0;
@override
double computeDistanceToActualBaseline(
TextBaseline baseline,
Map<_FracPos, RenderBox> childrenTable,
) =>
height;
@override
AxisConfiguration<_FracPos> performHorizontalIntrinsicLayout({
required Map<_FracPos, double> childrenWidths,
bool isComputingIntrinsics = false,
}) {
final numerSize = childrenWidths[_FracPos.numer]!;
final denomSize = childrenWidths[_FracPos.denom]!;
final barLength = math.max(numerSize, denomSize);
// KaTeX/src/katex.less
final nullDelimiterWidth = 0.12.cssEm.toLpUnder(options);
final width = barLength + 2 * nullDelimiterWidth;
if (!isComputingIntrinsics) {
this.barLength = barLength;
this.width = width;
}
return AxisConfiguration(
size: width,
offsetTable: {
_FracPos.numer: 0.5 * (width - numerSize),
_FracPos.denom: 0.5 * (width - denomSize),
},
);
}
@override
AxisConfiguration<_FracPos> performVerticalIntrinsicLayout({
required Map<_FracPos, double> childrenHeights,
required Map<_FracPos, double> childrenBaselines,
bool isComputingIntrinsics = false,
}) {
final numerSize = childrenHeights[_FracPos.numer]!;
final denomSize = childrenHeights[_FracPos.denom]!;
final numerHeight = childrenBaselines[_FracPos.numer]!;
final denomHeight = childrenBaselines[_FracPos.denom]!;
final metrics = options.fontMetrics;
final xi8 = metrics.defaultRuleThickness.cssEm.toLpUnder(options);
final theta = barSize?.toLpUnder(options) ?? xi8;
// Rule 15b
var u = (options.style > MathStyle.text
? metrics.num1
: (theta != 0 ? metrics.num2 : metrics.num3))
.cssEm
.toLpUnder(options);
var v = (options.style > MathStyle.text ? metrics.denom1 : metrics.denom2)
.cssEm
.toLpUnder(options);
final a = metrics.axisHeight.cssEm.toLpUnder(options);
final hx = numerHeight;
final dx = numerSize - numerHeight;
final hz = denomHeight;
final dz = denomSize - denomHeight;
if (theta == 0) {
// Rule 15c
final phi = options.style > MathStyle.text ? 7 * xi8 : 3 * xi8;
final psi = (u - dx) - (hz - v);
if (psi < phi) {
u += 0.5 * (phi - psi);
v += 0.5 * (phi - psi);
}
} else {
// Rule 15d
final phi = options.style > MathStyle.text ? 3 * theta : theta;
if (u - dx - a - 0.5 * theta < phi) {
u = phi + dx + a + 0.5 * theta;
}
if (a - 0.5 * theta - hz + v < phi) {
v = phi + hz - a + 0.5 * theta;
}
}
final height = hx + u;
final depth = dz + v;
if (!isComputingIntrinsics) {
this.height = height;
this.theta = theta;
this.a = a;
}
return AxisConfiguration(
size: height + depth,
offsetTable: {
_FracPos.numer: height - u - hx,
_FracPos.denom: height + v - hz,
},
);
}
@override
void additionalPaint(PaintingContext context, Offset offset) {
if (theta != 0) {
final paint = Paint()
..color = options.color
..strokeWidth = theta;
context.canvas.drawLine(
Offset(0.5 * (width - barLength), height - a) + offset,
Offset(0.5 * (width + barLength), height - a) + offset,
paint,
);
}
}
}