tex_text.dart
5.2 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
library tex_text;
import 'package:flutter/material.dart';
import 'package:flutter_math_fork/flutter_math.dart';
export 'package:flutter_math_fork/flutter_math.dart';
/// A LaTex text view.
///
/// Example:
/// ```dart
/// TexText(r"The equation is $x^2+y^2=z^2$") //Output: The equation is <LaTex formatted equation>
///
/// // \$ shows $ result
/// TexText(r"The equation is \$") //Output: The equation is $
/// ```
class TexText extends StatelessWidget {
const TexText(
this.text, {
super.key,
TextStyle? style,
this.mathStyle = MathStyle.text,
}) : _style = style;
final String text;
final TextStyle? _style;
final MathStyle mathStyle;
// final TexAlignment alignment;
/// LaTex to HTML parser
String toHtml() {
return toHtmlData(text);
}
/// LaTex custom syntax
static String _newEasySyntax(String data) {
return data
.replaceAll(r"\frac", r"\cfrac")
.replaceAll(r"\left[", r"[")
.replaceAll(r"\right]", r"]")
.replaceAll(r"[", r"{\left[{")
.replaceAll(r"]", r"}\right]}")
.replaceAllMapped(
RegExp(r'\\sqrt\{\\left\[\{(.*?)\}\\right\]\}'),
(match) =>
"\\sqrt[${match[1].toString().replaceAll(r"\cfrac", r"\frac")}]",
)
.replaceAll(r"\left\{", r"\{")
.replaceAll(r"\right\}", r"\}")
.replaceAll(r"\{", r"{\left\{{")
.replaceAll(r"\}", r"}\right\}}")
.replaceAll(r"\left(", r"(")
.replaceAll(r"\right)", r")")
.replaceAll(r"(", r"{\left({")
.replaceAll(r")", r"}\right)}")
.replaceAll(RegExp(r"\\tf"), r"\therefore")
.replaceAll(RegExp(r"\\bc"), r"\because")
.replaceAllMapped(
RegExp(r"([^A-Za-z\\]|^)(sin|cos|tan|cosec|sec|cot)([^A-Za-z]|$)"),
(match) =>
"${match[1].toString()}{\\,\\${match[2].toString()}}${match[3].toString()}")
.replaceAll(r"=>", r"{\Rightarrow}")
.replaceAll(RegExp(r"\\AA(\s|$)"), r"{\text{Å}}")
.replaceAll(r"*", r"{\times}")
.replaceAllMapped(
RegExp(r"\\([a-z]?)mat(\s+?\S.*?)\\([a-z]?)mat"),
(match) =>
"\\begin{${match[1].toString()}matrix}${match[2].toString().replaceAll("\\&", r"{\&}").replaceAll(",", "&").replaceAll("&&", "\\\\")}\\end{${match[3].toString()}matrix}");
}
/// LaTex to HTML parser
static String toHtmlData(String data) {
const dollar = r"\[-~`::36]";
return data.replaceAll("\\\$", dollar).splitMapJoin(
RegExp(
r"(?!\\)\$(.*?)(?!\\)\$",
),
onMatch: (p0) {
return "\\(${_newEasySyntax(p0[1].toString().replaceAll(dollar, "\\\$"))}\\)";
},
onNonMatch: (p0) {
return p0
.replaceAll(dollar, "\$")
.replaceAll("\n", "</br>")
.replaceAll(" ", r" ");
},
).trim();
}
/// Generate spans for widget
Widget _generateWidget(BuildContext context, String e) {
TextStyle? style = _style ?? Theme.of(context).textTheme.bodyMedium;
const dollar = r"\[-~`::36]";
List<InlineSpan> spans = [];
e.replaceAll("\\\$", dollar).splitMapJoin(
RegExp(
r"\$(.*?)\$",
),
onMatch: (p0) {
spans.add(
WidgetSpan(
alignment: PlaceholderAlignment.baseline,
baseline: TextBaseline.alphabetic,
child: Math.tex(
_newEasySyntax(p0[1].toString().replaceAll(dollar, "\\\$")),
textStyle: style?.copyWith(
fontFamily: "SansSerif",
),
mathStyle: mathStyle,
textScaleFactor: 1,
settings: const TexParserSettings(
strict: Strict.ignore,
),
options: MathOptions(
sizeUnderTextStyle: MathSize.large,
color: style?.color ?? Theme.of(context).colorScheme.onSurface,
fontSize: style?.fontSize ??
Theme.of(context).textTheme.bodyMedium?.fontSize,
mathFontOptions: FontOptions(
fontFamily: "Main",
fontWeight: style?.fontWeight ?? FontWeight.normal,
fontShape: FontStyle.normal,
),
textFontOptions: FontOptions(
fontFamily: "Main",
fontWeight: style?.fontWeight ?? FontWeight.normal,
fontShape: FontStyle.normal,
),
style: mathStyle,
),
onErrorFallback: (err) {
return Text(
"\$${p0[1]}\$",
style: style?.copyWith(
color: Theme.of(context).colorScheme.error) ??
TextStyle(color: Theme.of(context).colorScheme.error),
);
},
),
),
);
return p0[1].toString();
},
onNonMatch: (p0) {
spans.add(
TextSpan(
text: p0.toString().replaceAll(dollar, "\$"),
style: style,
),
);
return p0;
},
);
return Text.rich(
TextSpan(
children: spans,
),
);
}
@override
Widget build(BuildContext context) {
return _generateWidget(context, text);
}
}