sqrt.dart
13.5 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import 'dart:math' as math;
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import '../../render/constants.dart';
import '../../render/layout/custom_layout.dart';
import '../../render/layout/layout_builder_baseline.dart';
import '../../render/layout/min_dimension.dart';
import '../../render/layout/reset_baseline.dart';
import '../../render/svg/delimiter.dart';
import '../../render/svg/svg_geomertry.dart';
import '../../render/svg/svg_string.dart';
import '../../render/utils/render_box_offset.dart';
import '../../render/utils/render_box_layout.dart';
import '../options.dart';
import '../size.dart';
import '../style.dart';
import '../syntax_tree.dart';
/// Square root node.
///
/// Examples:
/// - Word: `\sqrt` `\sqrt(index & base)`
/// - Latex: `\sqrt` `\sqrt[index]{base}`
/// - MathML: `msqrt` `mroot`
class SqrtNode extends SlotableNode {
/// The index.
final EquationRowNode? index;
/// The sqrt-and.
final EquationRowNode base;
SqrtNode({
required this.index,
required this.base,
});
@override
BuildResult buildWidget(
MathOptions options, List<BuildResult?> childBuildResults) {
final baseResult = childBuildResults[1]!;
final indexResult = childBuildResults[0];
return BuildResult(
options: options,
widget: CustomLayout<_SqrtPos>(
delegate: SqrtLayoutDelegate(
options: options,
baseOptions: baseResult.options,
// indexOptions: indexResult?.options,
),
children: <Widget>[
CustomLayoutId(
id: _SqrtPos.base,
child: MinDimension(
minHeight: options.fontMetrics.xHeight.cssEm.toLpUnder(options),
topPadding: 0,
child: baseResult.widget,
),
),
CustomLayoutId(
id: _SqrtPos.surd,
// we use IgnorePointer here to ignore root image during hit test,
// so 'SelectionManagerMixin.getRenderLineAtOffset' can find
// render lines in the base widget
child: IgnorePointer(
child: LayoutBuilderPreserveBaseline(
builder: (context, constraints) => sqrtSvg(
minDelimiterHeight: constraints.minHeight,
baseWidth: constraints.minWidth,
options: options,
),
),
),
),
if (index != null)
CustomLayoutId(
id: _SqrtPos.ind,
child: indexResult!.widget,
),
],
),
);
}
@override
List<MathOptions> computeChildOptions(MathOptions options) => [
options.havingStyle(MathStyle.scriptscript),
options.havingStyle(options.style.cramp()),
];
@override
List<EquationRowNode?> computeChildren() => [index, base];
@override
AtomType get leftType => AtomType.ord;
@override
AtomType get rightType => AtomType.ord;
@override
bool shouldRebuildWidget(MathOptions oldOptions, MathOptions newOptions) =>
false;
@override
SqrtNode updateChildren(List<EquationRowNode?> newChildren) => SqrtNode(
index: newChildren[0],
base: newChildren[1]!,
);
@override
Map<String, Object?> toJson() => super.toJson()
..addAll({
'index': index?.toJson(),
'base': base.toJson(),
});
SqrtNode copyWith({
EquationRowNode? index,
EquationRowNode? base,
}) =>
SqrtNode(
index: index ?? this.index,
base: base ?? this.base,
);
}
enum _SqrtPos {
base,
ind, // Name collision here
surd,
}
// Square roots are handled in the TeXbook pg. 443, Rule 11.
class SqrtLayoutDelegate extends CustomLayoutDelegate<_SqrtPos> {
final MathOptions options;
final MathOptions baseOptions;
// final MathOptions indexOptions;
SqrtLayoutDelegate({
required this.options,
required this.baseOptions,
// required this.indexOptions,
});
var heightAboveBaseline = 0.0;
var svgHorizontalPos = 0.0;
var svgVerticalPos = 0.0;
@override
double computeDistanceToActualBaseline(
TextBaseline baseline, Map<_SqrtPos, RenderBox> childrenTable) =>
heightAboveBaseline;
@override
double getIntrinsicSize({
required Axis sizingDirection,
required bool max,
required double extent,
required double Function(RenderBox child, double extent) childSize,
required Map<_SqrtPos, RenderBox> childrenTable,
}) =>
0;
@override
Size computeLayout(
BoxConstraints constraints,
Map<_SqrtPos, RenderBox> childrenTable, {
bool dry = true,
}) {
final base = childrenTable[_SqrtPos.base]!;
final index = childrenTable[_SqrtPos.ind];
final surd = childrenTable[_SqrtPos.surd]!;
final Size baseSize = base.getLayoutSize(infiniteConstraint, dry: dry);
final Size indexSize = index?.getLayoutSize(
infiniteConstraint,
dry: dry,
) ??
Size.zero;
final baseHeight = dry ? 0 : base.layoutHeight;
final baseWidth = baseSize.width;
final indexHeight = dry ? 0 : index?.layoutHeight ?? 0.0;
final indexWidth = indexSize.width;
final theta = baseOptions.fontMetrics.defaultRuleThickness.cssEm
.toLpUnder(baseOptions);
var phi = baseOptions.style > MathStyle.text
? baseOptions.fontMetrics.xHeight.cssEm.toLpUnder(baseOptions)
: theta;
var psi = theta + 0.25 * phi.abs();
final minSqrtHeight = baseSize.height + psi + theta;
final surdConstraints = BoxConstraints(
minWidth: baseWidth,
minHeight: minSqrtHeight,
);
final Size surdSize = surd.getLayoutSize(surdConstraints, dry: dry);
final advanceWidth = getSqrtAdvanceWidth(minSqrtHeight, baseWidth, options);
// Parameters for index
// from KaTeX/src/katex.less
final indexRightPadding = -10.0.mu.toLpUnder(options);
// KaTeX chose a way to large value (5mu). We will use a smaller one.
final indexLeftPadding = 0.5.pt.toLpUnder(options);
// Horizontal layout
final sqrtHorizontalPos =
math.max(0.0, indexLeftPadding + indexSize.width + indexRightPadding);
final width = sqrtHorizontalPos + surdSize.width;
// Vertical layout
final ruleWidth = dry ? 0 : surd.layoutHeight;
if (!dry) {
final delimDepth = dry ? surdSize.height : surd.layoutDepth;
if (delimDepth > baseSize.height + psi) {
psi += 0.5 * (delimDepth - baseSize.height - psi);
}
}
final bodyHeight = baseHeight + psi + ruleWidth;
final bodyDepth = surdSize.height - bodyHeight;
final indexShift = 0.6 * (bodyHeight - bodyDepth);
final sqrtVerticalPos =
math.max(0.0, indexHeight + indexShift - baseHeight - psi - ruleWidth);
final height = sqrtVerticalPos + surdSize.height;
// Position children
if (!dry) {
svgHorizontalPos = sqrtHorizontalPos;
heightAboveBaseline = bodyHeight + sqrtVerticalPos;
base.offset = Offset(
sqrtHorizontalPos + advanceWidth, heightAboveBaseline - baseHeight);
index?.offset = Offset(sqrtHorizontalPos - indexRightPadding - indexWidth,
heightAboveBaseline - indexShift - indexHeight);
surd.offset = Offset(sqrtHorizontalPos, sqrtVerticalPos);
}
return Size(width, height);
}
}
const sqrtDelimieterSequence = [
// DelimiterConf(mainRegular, MathStyle.scriptscript),
// DelimiterConf(mainRegular, MathStyle.script),
DelimiterConf(mainRegular, MathStyle.text),
DelimiterConf(size1Regular, MathStyle.text),
DelimiterConf(size2Regular, MathStyle.text),
DelimiterConf(size3Regular, MathStyle.text),
DelimiterConf(size4Regular, MathStyle.text),
];
const vbPad = 80;
const emPad = vbPad / 1000;
// We use a different strategy of picking \\surd font than KaTeX
// KaTeX chooses the style and font of the \\surd to cover inner at *normalsize*
// We will use a highly similar strategy while sticking to the strict meaning
// of TexBook Rule 11. We do not choose the style at *normalsize*
double getSqrtAdvanceWidth(
double minDelimiterHeight, double baseWidth, MathOptions options) {
// final newOptions = options.havingBaseSize();
final delimConf = sqrtDelimieterSequence.firstWhereOrNull(
(element) =>
getHeightForDelim(
delim: '\u221A', // √
fontName: element.font.fontName,
style: element.style,
options: options,
) >
minDelimiterHeight,
);
if (delimConf != null) {
final delimOptions = options.havingStyle(delimConf.style);
if (delimConf.font.fontName == 'Main-Regular') {
final advanceWidth = 0.833.cssEm.toLpUnder(delimOptions);
return advanceWidth;
} else {
// We will directly apply corresponding font
final advanceWidth = 1.0.cssEm.toLpUnder(delimOptions);
return advanceWidth;
}
} else {
final advanceWidth = 1.056.cssEm.toLpUnder(options);
return advanceWidth;
}
}
// We use a different strategy of picking \\surd font than KaTeX
// KaTeX chooses the style and font of the \\surd to cover inner at *normalsize*
// We will use a highly similar strategy while sticking to the strict meaning
// of TexBook Rule 11. We do not choose the style at *normalsize*
Widget sqrtSvg({
required double minDelimiterHeight,
required double baseWidth,
required MathOptions options,
}) {
// final newOptions = options.havingBaseSize();
final delimConf = sqrtDelimieterSequence.firstWhereOrNull(
(element) =>
getHeightForDelim(
delim: '\u221A', // √
fontName: element.font.fontName,
style: element.style,
options: options,
) >
minDelimiterHeight,
);
final extraViniculum = 0.0; //math.max(0.0, options)
// final ruleWidth =
// options.fontMetrics.sqrtRuleThickness.cssEm.toLpUnder(options);
// TODO: support Settings.minRuleThickness.
// These are the known height + depth for \u221A
if (delimConf != null) {
final fontHeight = const {
'Main-Regular': 1.0,
'Size1-Regular': 1.2,
'Size2-Regular': 1.8,
'Size3-Regular': 2.4,
'Size4-Regular': 3.0,
}[delimConf.font.fontName]!;
final delimOptions = options.havingStyle(delimConf.style);
final viewPortHeight =
(fontHeight + extraViniculum + emPad).cssEm.toLpUnder(delimOptions);
if (delimConf.font.fontName == 'Main-Regular') {
// We will be vertically stretching the sqrtMain path (by viewPort vs
// viewBox) to mimic the height of \u221A under Main-Regular font and
// corresponding Mathstyle.
final advanceWidth = 0.833.cssEm.toLpUnder(delimOptions);
final viewPortWidth = advanceWidth + baseWidth;
final viewBoxHeight = 1000 + 1000 * extraViniculum + vbPad;
final viewBoxWidth = viewPortWidth.lp.toCssEmUnder(delimOptions) * 1000;
final svgPath = sqrtPath('sqrtMain', extraViniculum, viewBoxHeight);
return ResetBaseline(
height: (options.fontMetrics.sqrtRuleThickness + extraViniculum)
.cssEm
.toLpUnder(delimOptions),
child: MinDimension(
topPadding: -emPad.cssEm.toLpUnder(delimOptions),
child: svgWidgetFromPath(
svgPath,
Size(viewPortWidth, viewPortHeight),
Rect.fromLTWH(0, 0, viewBoxWidth, viewBoxHeight),
options.color,
align: Alignment.topLeft,
fit: BoxFit.fill,
),
),
);
} else {
// We will directly apply corresponding font
final advanceWidth = 1.0.cssEm.toLpUnder(delimOptions);
final viewPortWidth = math.max(
advanceWidth + baseWidth,
1.02.cssEm.toCssEmUnder(delimOptions),
);
final viewBoxHeight = (1000 + vbPad) * fontHeight;
final viewBoxWidth = viewPortWidth.lp.toCssEmUnder(delimOptions) * 1000;
final svgPath = sqrtPath('sqrt${delimConf.font.fontName.substring(0, 5)}',
extraViniculum, viewBoxHeight);
return ResetBaseline(
height: (options.fontMetrics.sqrtRuleThickness + extraViniculum)
.cssEm
.toLpUnder(delimOptions),
child: MinDimension(
topPadding: -emPad.cssEm.toLpUnder(delimOptions),
child: svgWidgetFromPath(
svgPath,
Size(viewPortWidth, viewPortHeight),
Rect.fromLTWH(0, 0, viewBoxWidth, viewBoxHeight),
options.color,
align: Alignment.topLeft,
fit: BoxFit
.cover, // BoxFit.fitHeight, // For DomCanvas compatibility
),
),
);
}
} else {
// We will use the viewBoxHeight parameter in sqrtTall path
final viewPortHeight =
minDelimiterHeight + (extraViniculum + emPad).cssEm.toLpUnder(options);
final viewBoxHeight = 1000 * minDelimiterHeight.lp.toCssEmUnder(options) +
extraViniculum +
vbPad;
final advanceWidth = 1.056.cssEm.toLpUnder(options);
final viewPortWidth = advanceWidth + baseWidth;
final viewBoxWidth = viewPortWidth.lp.toCssEmUnder(options) * 1000;
final svgPath = sqrtPath('sqrtTall', extraViniculum, viewBoxHeight);
return ResetBaseline(
height: (options.fontMetrics.sqrtRuleThickness + extraViniculum)
.cssEm
.toLpUnder(options),
child: MinDimension(
topPadding: -emPad.cssEm.toLpUnder(options),
child: svgWidgetFromPath(
svgPath,
Size(viewPortWidth, viewPortHeight),
Rect.fromLTWH(0, 0, viewBoxWidth, viewBoxHeight),
options.color,
align: Alignment.topLeft,
fit: BoxFit.cover, // BoxFit.fitHeight, // For DomCanvas compatibility
),
),
);
}
}