left_right.dart
11.3 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import 'dart:math' as math;
import 'package:collection/collection.dart';
import 'package:flutter/widgets.dart';
import '../../font/metrics/font_metrics.dart';
import '../../render/constants.dart';
import '../../render/layout/layout_builder_baseline.dart';
import '../../render/layout/line.dart';
import '../../render/layout/shift_baseline.dart';
import '../../render/svg/delimiter.dart';
import '../../render/symbols/make_symbol.dart';
import '../options.dart';
import '../size.dart';
import '../spacing.dart';
import '../syntax_tree.dart';
import '../types.dart';
/// Left right node.
class LeftRightNode extends SlotableNode<EquationRowNode> {
/// Unicode symbol for the left delimiter character.
final String? leftDelim;
/// Unicode symbol for the right delimiter character.
final String? rightDelim;
/// List of inside bodys.
///
/// Its length should be 1 longer than [middle].
final List<EquationRowNode> body;
/// List of middle delimiter characters.
final List<String?> middle;
LeftRightNode({
required this.leftDelim,
required this.rightDelim,
required this.body,
this.middle = const [],
}) : assert(body.isNotEmpty),
assert(middle.length == body.length - 1);
@override
BuildResult buildWidget(
MathOptions options, List<BuildResult?> childBuildResults) {
final numElements = 2 + body.length + middle.length;
final a = options.fontMetrics.axisHeight.cssEm.toLpUnder(options);
final childWidgets = List.generate(numElements, (index) {
if (index % 2 == 0) {
// Delimiter
return LineElement(
customCrossSize: (height, depth) {
final delta = math.max(height - a, depth + a);
final delimeterFullHeight = math.max(delta / 500 * delimiterFactor,
2 * delta - delimiterShorfall.toLpUnder(options));
return BoxConstraints(minHeight: delimeterFullHeight);
},
trailingMargin: index == numElements - 1
? 0.0
: getSpacingSize(index == 0 ? AtomType.open : AtomType.rel,
body[(index + 1) ~/ 2].leftType, options.style)
.toLpUnder(options),
child: LayoutBuilderPreserveBaseline(
builder: (context, constraints) => buildCustomSizedDelimWidget(
index == 0
? leftDelim
: index == numElements - 1
? rightDelim
: middle[index ~/ 2 - 1],
constraints.minHeight,
options,
),
),
);
} else {
// Content
return LineElement(
trailingMargin: getSpacingSize(
body[index ~/ 2].rightType,
index == numElements - 2 ? AtomType.close : AtomType.rel,
options.style)
.toLpUnder(options),
child: childBuildResults[index ~/ 2]!.widget,
);
}
}, growable: false);
return BuildResult(
options: options,
widget: Line(
children: childWidgets,
),
);
}
@override
List<MathOptions> computeChildOptions(MathOptions options) =>
List.filled(body.length, options, growable: false);
@override
List<EquationRowNode> computeChildren() => body;
@override
AtomType get leftType => AtomType.open;
@override
AtomType get rightType => AtomType.close;
@override
bool shouldRebuildWidget(MathOptions oldOptions, MathOptions newOptions) =>
false;
@override
LeftRightNode updateChildren(List<EquationRowNode> newChildren) =>
LeftRightNode(
leftDelim: leftDelim,
rightDelim: rightDelim,
body: newChildren,
middle: middle,
);
@override
Map<String, Object?> toJson() => super.toJson()
..addAll({
'body': body.map((e) => e.toJson()),
'leftDelim': leftDelim,
'rightDelim': rightDelim,
if (middle.isNotEmpty) 'middle': middle,
});
}
// TexBook Appendix B
const delimiterFactor = 901;
const delimiterShorfall = Measurement(value: 5.0, unit: Unit.pt);
const stackLargeDelimiters = {
'(', ')',
'[', ']',
'{', '}',
'\u230a', '\u230b', // '\\lfloor', '\\rfloor',
'\u2308', '\u2309', // '\\lceil', '\\rceil',
'\u221a', // '\\surd'
};
// delimiters that always stack
const stackAlwaysDelimiters = {
'\u2191', // '\\uparrow',
'\u2193', // '\\downarrow',
'\u2195', // '\\updownarrow',
'\u21d1', // '\\Uparrow',
'\u21d3', // '\\Downarrow',
'\u21d5', // '\\Updownarrow',
'|',
// '\\|',
// '\\vert',
'\u2016', // '\\Vert', '\u2225'
'\u2223', // '\\lvert', '\\rvert', '\\mid'
'\u2225', // '\\lVert', '\\rVert',
'\u27ee', // '\\lgroup',
'\u27ef', // '\\rgroup',
'\u23b0', // '\\lmoustache',
'\u23b1', // '\\rmoustache',
};
// and delimiters that never stack
const stackNeverDelimiters = {
'\u27e8', //'<',
'\u27e9', //'>',
'/',
};
Widget buildCustomSizedDelimWidget(
String? delim, double minDelimiterHeight, MathOptions options) {
if (delim == null) {
final axisHeight = options.fontMetrics.xHeight.cssEm.toLpUnder(options);
return ShiftBaseline(
relativePos: 0.5,
offset: axisHeight,
child: Container(
height: minDelimiterHeight,
width: nullDelimiterSpace.toLpUnder(options),
),
);
}
List<DelimiterConf> sequence;
if (stackNeverDelimiters.contains(delim)) {
sequence = stackNeverDelimiterSequence;
} else if (stackLargeDelimiters.contains(delim)) {
sequence = stackLargeDelimiterSequence;
} else {
sequence = stackAlwaysDelimiterSequence;
}
var delimConf = sequence.firstWhereOrNull((element) =>
getHeightForDelim(
delim: delim,
fontName: element.font.fontName,
style: element.style,
options: options,
) >
minDelimiterHeight);
if (stackNeverDelimiters.contains(delim)) {
delimConf ??= sequence.last;
}
if (delimConf != null) {
final axisHeight = options.fontMetrics.axisHeight.cssEm.toLpUnder(options);
return ShiftBaseline(
relativePos: 0.5,
offset: axisHeight,
child: makeChar(delim, delimConf.font,
lookupChar(delim, delimConf.font, Mode.math), options),
);
} else {
return makeStackedDelim(delim, minDelimiterHeight, Mode.math, options);
}
}
Widget makeStackedDelim(
String delim, double minDelimiterHeight, Mode mode, MathOptions options) {
final conf = stackDelimiterConfs[delim]!;
final topMetrics = lookupChar(conf.top, conf.font, Mode.math)!;
final repeatMetrics = lookupChar(conf.repeat, conf.font, Mode.math)!;
final bottomMetrics = lookupChar(conf.bottom, conf.font, Mode.math)!;
final topHeight =
(topMetrics.height + topMetrics.depth).cssEm.toLpUnder(options);
final repeatHeight =
(repeatMetrics.height + repeatMetrics.depth).cssEm.toLpUnder(options);
final bottomHeight =
(bottomMetrics.height + bottomMetrics.depth).cssEm.toLpUnder(options);
var middleHeight = 0.0;
var middleFactor = 1;
CharacterMetrics? middleMetrics;
if (conf.middle != null) {
middleMetrics = lookupChar(conf.middle!, conf.font, Mode.math)!;
middleHeight =
(middleMetrics.height + middleMetrics.depth).cssEm.toLpUnder(options);
middleFactor = 2;
}
final minHeight = topHeight + bottomHeight + middleHeight;
final repeatCount = math
.max(0, (minDelimiterHeight - minHeight) / (repeatHeight * middleFactor))
.ceil();
// final realHeight = minHeight + repeatCount * middleFactor * repeatHeight;
final axisHeight = options.fontMetrics.axisHeight.cssEm.toLpUnder(options);
return ShiftBaseline(
relativePos: 0.5,
offset: axisHeight,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
makeChar(conf.top, conf.font, topMetrics, options),
for (var i = 0; i < repeatCount; i++)
makeChar(conf.repeat, conf.font, repeatMetrics, options),
if (conf.middle != null)
makeChar(conf.middle!, conf.font, middleMetrics!, options),
if (conf.middle != null)
for (var i = 0; i < repeatCount; i++)
makeChar(conf.repeat, conf.font, repeatMetrics, options),
makeChar(conf.bottom, conf.font, bottomMetrics, options),
],
),
);
}
const size4Font = FontOptions(fontFamily: 'Size4');
const size1Font = FontOptions(fontFamily: 'Size1');
class StackDelimiterConf {
final String top;
final String? middle;
final String repeat;
final String bottom;
final FontOptions font;
const StackDelimiterConf({
required this.top,
this.middle,
required this.repeat,
required this.bottom,
this.font = size4Font,
});
}
const stackDelimiterConfs = {
'\u2191': // '\\uparrow',
StackDelimiterConf(
top: '\u2191', repeat: '\u23d0', bottom: '\u23d0', font: size1Font),
'\u2193': // '\\downarrow',
StackDelimiterConf(
top: '\u23d0', repeat: '\u23d0', bottom: '\u2193', font: size1Font),
'\u2195': // '\\updownarrow',
StackDelimiterConf(
top: '\u2191', repeat: '\u23d0', bottom: '\u2193', font: size1Font),
'\u21d1': // '\\Uparrow',
StackDelimiterConf(
top: '\u21d1', repeat: '\u2016', bottom: '\u2016', font: size1Font),
'\u21d3': // '\\Downarrow',
StackDelimiterConf(
top: '\u2016', repeat: '\u2016', bottom: '\u21d3', font: size1Font),
'\u21d5': // '\\Updownarrow',
StackDelimiterConf(
top: '\u21d1', repeat: '\u2016', bottom: '\u21d3', font: size1Font),
'|': // '\\|' ,'\\vert',
StackDelimiterConf(
top: '\u2223', repeat: '\u2223', bottom: '\u2223', font: size1Font),
'\u2016': // '\\Vert', '\u2225'
StackDelimiterConf(
top: '\u2016', repeat: '\u2016', bottom: '\u2016', font: size1Font),
'\u2223': // '\\lvert', '\\rvert', '\\mid'
StackDelimiterConf(
top: '\u2223', repeat: '\u2223', bottom: '\u2223', font: size1Font),
'\u2225': // '\\lVert', '\\rVert',
StackDelimiterConf(
top: '\u2225', repeat: '\u2225', bottom: '\u2225', font: size1Font),
'(': StackDelimiterConf(top: '\u239b', repeat: '\u239c', bottom: '\u239d'),
')': StackDelimiterConf(top: '\u239e', repeat: '\u239f', bottom: '\u23a0'),
'[': StackDelimiterConf(top: '\u23a1', repeat: '\u23a2', bottom: '\u23a3'),
']': StackDelimiterConf(top: '\u23a4', repeat: '\u23a5', bottom: '\u23a6'),
'{': StackDelimiterConf(
top: '\u23a7', middle: '\u23a8', bottom: '\u23a9', repeat: '\u23aa'),
'}': StackDelimiterConf(
top: '\u23ab', middle: '\u23ac', bottom: '\u23ad', repeat: '\u23aa'),
'\u230a': // '\\lfloor',
StackDelimiterConf(top: '\u23a2', repeat: '\u23a2', bottom: '\u23a3'),
'\u230b': // '\\rfloor',
StackDelimiterConf(top: '\u23a5', repeat: '\u23a5', bottom: '\u23a6'),
'\u2308': // '\\lceil',
StackDelimiterConf(top: '\u23a1', repeat: '\u23a2', bottom: '\u23a2'),
'\u2309': // '\\rceil',
StackDelimiterConf(top: '\u23a4', repeat: '\u23a5', bottom: '\u23a5'),
'\u27ee': // '\\lgroup',
StackDelimiterConf(top: '\u23a7', repeat: '\u23aa', bottom: '\u23a9'),
'\u27ef': // '\\rgroup',
StackDelimiterConf(top: '\u23ab', repeat: '\u23aa', bottom: '\u23ad'),
'\u23b0': // '\\lmoustache',
StackDelimiterConf(top: '\u23a7', repeat: '\u23aa', bottom: '\u23ad'),
'\u23b1': // '\\rmoustache',
StackDelimiterConf(top: '\u23ab', repeat: '\u23aa', bottom: '\u23a9'),
};