From 2732beb2b88105aa1bf281b891509f038a6986df Mon Sep 17 00:00:00 2001
From: Rajesh Malviya
Date: Thu, 24 Apr 2025 11:37:46 +0530
Subject: [PATCH 01/13] content test [nfc]: Use const for math block tests
---
test/model/content_test.dart | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/test/model/content_test.dart b/test/model/content_test.dart
index 5ab60c8e7e..6a0bc6ebe7 100644
--- a/test/model/content_test.dart
+++ b/test/model/content_test.dart
@@ -529,7 +529,7 @@ class ContentExample {
]),
]));
- static final mathBlock = ContentExample(
+ static const mathBlock = ContentExample(
'math block',
"```math\n\\lambda\n```",
expectedText: r'\lambda',
@@ -549,7 +549,7 @@ class ContentExample {
]),
])]);
- static final mathBlocksMultipleInParagraph = ContentExample(
+ static const mathBlocksMultipleInParagraph = ContentExample(
'math blocks, multiple in paragraph',
'```math\na\n\nb\n```',
// https://chat.zulip.org/#narrow/channel/7-test-here/topic/.E2.9C.94.20Rajesh/near/2001490
@@ -586,7 +586,7 @@ class ContentExample {
]),
]);
- static final mathBlockInQuote = ContentExample(
+ static const mathBlockInQuote = ContentExample(
'math block in quote',
// There's sometimes a quirky extra `
\n` at the end of the `` that
// encloses the math block. In particular this happens when the math block
@@ -614,7 +614,7 @@ class ContentExample {
]),
])]);
- static final mathBlocksMultipleInQuote = ContentExample(
+ static const mathBlocksMultipleInQuote = ContentExample(
'math blocks, multiple in quote',
"````quote\n```math\na\n\nb\n```\n````",
// https://chat.zulip.org/#narrow/channel/7-test-here/topic/.E2.9C.94.20Rajesh/near/2029236
@@ -654,7 +654,7 @@ class ContentExample {
]),
])]);
- static final mathBlockBetweenImages = ContentExample(
+ static const mathBlockBetweenImages = ContentExample(
'math block between images',
// https://chat.zulip.org/#narrow/channel/7-test-here/topic/Greg/near/2035891
'https://upload.wikimedia.org/wikipedia/commons/7/78/Verregende_bloem_van_een_Helenium_%27El_Dorado%27._22-07-2023._%28d.j.b%29.jpg\n```math\na\n```\nhttps://upload.wikimedia.org/wikipedia/commons/thumb/7/71/Zaadpluizen_van_een_Clematis_texensis_%27Princess_Diana%27._18-07-2023_%28actm.%29_02.jpg/1280px-Zaadpluizen_van_een_Clematis_texensis_%27Princess_Diana%27._18-07-2023_%28actm.%29_02.jpg',
@@ -702,7 +702,7 @@ class ContentExample {
// The font sizes can be compared using the katex.css generated
// from katex.scss :
// https://unpkg.com/katex@0.16.21/dist/katex.css
- static final mathBlockKatexSizing = ContentExample(
+ static const mathBlockKatexSizing = ContentExample(
'math block; KaTeX different sizing',
// https://chat.zulip.org/#narrow/channel/7-test-here/topic/Rajesh/near/2155476
'```math\n\\Huge 1\n\\huge 2\n\\LARGE 3\n\\Large 4\n\\large 5\n\\normalsize 6\n\\small 7\n\\footnotesize 8\n\\scriptsize 9\n\\tiny 0\n```',
@@ -779,7 +779,7 @@ class ContentExample {
]),
]);
- static final mathBlockKatexNestedSizing = ContentExample(
+ static const mathBlockKatexNestedSizing = ContentExample(
'math block; KaTeX nested sizing',
'```math\n\\tiny {1 \\Huge 2}\n```',
'
'
@@ -821,7 +821,7 @@ class ContentExample {
]),
]);
- static final mathBlockKatexDelimSizing = ContentExample(
+ static const mathBlockKatexDelimSizing = ContentExample(
'math block; KaTeX delimiter sizing',
// https://chat.zulip.org/#narrow/channel/7-test-here/topic/Rajesh/near/2147135
'```math\n⟨ \\big( \\Big[ \\bigg⌈ \\Bigg⌊\n```',
From 860dd00086214cb190feac0090c3a51cd3baecdf Mon Sep 17 00:00:00 2001
From: Rajesh Malviya
Date: Thu, 8 May 2025 21:04:10 +0530
Subject: [PATCH 02/13] content [nfc]: Inline _logError in
_KatexParser._parseSpan
This will prevent string interpolation being evaluated during
release build. Especially useful in later commit where it becomes
more expensive.
---
lib/model/katex.dart | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/lib/model/katex.dart b/lib/model/katex.dart
index 709f91b4b2..7c4b2deb1b 100644
--- a/lib/model/katex.dart
+++ b/lib/model/katex.dart
@@ -109,11 +109,6 @@ class _KatexParser {
bool get hasError => _hasError;
bool _hasError = false;
- void _logError(String message) {
- assert(debugLog(message));
- _hasError = true;
- }
-
List parseKatexHtml(dom.Element element) {
assert(element.localName == 'span');
assert(element.className == 'katex-html');
@@ -334,7 +329,8 @@ class _KatexParser {
break;
default:
- _logError('KaTeX: Unsupported CSS class: $spanClass');
+ assert(debugLog('KaTeX: Unsupported CSS class: $spanClass'));
+ _hasError = true;
}
}
final styles = KatexSpanStyles(
From 0a47cc55c826067ff9a7c3467465fa0df2506362 Mon Sep 17 00:00:00 2001
From: Rajesh Malviya
Date: Thu, 8 May 2025 21:34:09 +0530
Subject: [PATCH 03/13] content [nfc]: Refactor _KatexParser._parseChildSpans
to take list of nodes
---
lib/model/katex.dart | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/lib/model/katex.dart b/lib/model/katex.dart
index 7c4b2deb1b..f6375f907e 100644
--- a/lib/model/katex.dart
+++ b/lib/model/katex.dart
@@ -112,11 +112,11 @@ class _KatexParser {
List parseKatexHtml(dom.Element element) {
assert(element.localName == 'span');
assert(element.className == 'katex-html');
- return _parseChildSpans(element);
+ return _parseChildSpans(element.nodes);
}
- List _parseChildSpans(dom.Element element) {
- return List.unmodifiable(element.nodes.map((node) {
+ List _parseChildSpans(List nodes) {
+ return List.unmodifiable(nodes.map((node) {
if (node case dom.Element(localName: 'span')) {
return _parseSpan(node);
} else {
@@ -346,7 +346,7 @@ class _KatexParser {
if (element.nodes case [dom.Text(:final data)]) {
text = data;
} else {
- spans = _parseChildSpans(element);
+ spans = _parseChildSpans(element.nodes);
}
if (text == null && spans == null) throw KatexHtmlParseError();
From 4c621811312434d208f3517de936f5b1e1562373 Mon Sep 17 00:00:00 2001
From: Rajesh Malviya
Date: Tue, 22 Apr 2025 18:01:46 +0530
Subject: [PATCH 04/13] content: Scale inline KaTeX content based on the
surrounding text
This applies the correct font scaling if the KaTeX content is
inside a header.
---
lib/widgets/content.dart | 32 ++++++++++++++++++++++----------
test/widgets/content_test.dart | 30 ++++++++++++++++++++----------
2 files changed, 42 insertions(+), 20 deletions(-)
diff --git a/lib/widgets/content.dart b/lib/widgets/content.dart
index 40b510305d..bb60fc9be6 100644
--- a/lib/widgets/content.dart
+++ b/lib/widgets/content.dart
@@ -817,24 +817,37 @@ class MathBlock extends StatelessWidget {
children: [TextSpan(text: node.texSource)])));
}
- return _Katex(inline: false, nodes: nodes);
+ return _Katex(
+ inline: false,
+ textStyle: ContentTheme.of(context).textStylePlainParagraph,
+ nodes: nodes);
}
}
-// Base text style from .katex class in katex.scss :
-// https://github.com/KaTeX/KaTeX/blob/613c3da8/src/styles/katex.scss#L13-L15
-const kBaseKatexTextStyle = TextStyle(
- fontSize: kBaseFontSize * 1.21,
- fontFamily: 'KaTeX_Main',
- height: 1.2);
+/// Creates a base text style for rendering KaTeX content.
+///
+/// This applies the CSS styles defined in .katex class in katex.scss :
+/// https://github.com/KaTeX/KaTeX/blob/613c3da8/src/styles/katex.scss#L13-L15
+///
+/// Requires the [style.fontSize] to be non-null.
+TextStyle mkBaseKatexTextStyle(TextStyle style) {
+ return style.copyWith(
+ fontSize: style.fontSize! * 1.21,
+ fontFamily: 'KaTeX_Main',
+ height: 1.2,
+ fontWeight: FontWeight.normal,
+ fontStyle: FontStyle.normal);
+}
class _Katex extends StatelessWidget {
const _Katex({
required this.inline,
+ required this.textStyle,
required this.nodes,
});
final bool inline;
+ final TextStyle textStyle;
final List nodes;
@override
@@ -851,8 +864,7 @@ class _Katex extends StatelessWidget {
return Directionality(
textDirection: TextDirection.ltr,
child: DefaultTextStyle(
- style: kBaseKatexTextStyle.copyWith(
- color: ContentTheme.of(context).textStylePlainParagraph.color),
+ style: mkBaseKatexTextStyle(textStyle),
child: widget));
}
}
@@ -1262,7 +1274,7 @@ class _InlineContentBuilder {
: WidgetSpan(
alignment: PlaceholderAlignment.baseline,
baseline: TextBaseline.alphabetic,
- child: _Katex(inline: true, nodes: nodes));
+ child: _Katex(inline: true, textStyle: widget.style, nodes: nodes));
case GlobalTimeNode():
return WidgetSpan(alignment: PlaceholderAlignment.middle,
diff --git a/test/widgets/content_test.dart b/test/widgets/content_test.dart
index a788225aac..84c2b15ad2 100644
--- a/test/widgets/content_test.dart
+++ b/test/widgets/content_test.dart
@@ -595,15 +595,19 @@ void main() {
final content = ContentExample.mathBlockKatexSizing;
await prepareContent(tester, plainContent(content.html));
+ final context = tester.element(find.byType(MathBlock));
+ final baseTextStyle =
+ mkBaseKatexTextStyle(ContentTheme.of(context).textStylePlainParagraph);
+
final mathBlockNode = content.expectedNodes.single as MathBlockNode;
final baseNode = mathBlockNode.nodes!.single;
final nodes = baseNode.nodes!.skip(1); // Skip .strut node.
for (final katexNode in nodes) {
- final fontSize = katexNode.styles.fontSizeEm! * kBaseKatexTextStyle.fontSize!;
+ final fontSize = katexNode.styles.fontSizeEm! * baseTextStyle.fontSize!;
checkKatexText(tester, katexNode.text!,
fontFamily: 'KaTeX_Main',
fontSize: fontSize,
- fontHeight: kBaseKatexTextStyle.height!);
+ fontHeight: baseTextStyle.height!);
}
});
@@ -616,17 +620,21 @@ void main() {
final content = ContentExample.mathBlockKatexNestedSizing;
await prepareContent(tester, plainContent(content.html));
- var fontSize = 0.5 * kBaseKatexTextStyle.fontSize!;
+ final context = tester.element(find.byType(MathBlock));
+ final baseTextStyle =
+ mkBaseKatexTextStyle(ContentTheme.of(context).textStylePlainParagraph);
+
+ var fontSize = 0.5 * baseTextStyle.fontSize!;
checkKatexText(tester, '1',
fontFamily: 'KaTeX_Main',
fontSize: fontSize,
- fontHeight: kBaseKatexTextStyle.height!);
+ fontHeight: baseTextStyle.height!);
fontSize = 4.976 * fontSize;
checkKatexText(tester, '2',
fontFamily: 'KaTeX_Main',
fontSize: fontSize,
- fontHeight: kBaseKatexTextStyle.height!);
+ fontHeight: baseTextStyle.height!);
});
testWidgets('displays KaTeX content with different delimiter sizing', (tester) async {
@@ -642,13 +650,15 @@ void main() {
final baseNode = mathBlockNode.nodes!.single;
var nodes = baseNode.nodes!.skip(1); // Skip .strut node.
- final fontSize = kBaseKatexTextStyle.fontSize!;
+ final context = tester.element(find.byType(MathBlock));
+ final baseTextStyle =
+ mkBaseKatexTextStyle(ContentTheme.of(context).textStylePlainParagraph);
final firstNode = nodes.first;
checkKatexText(tester, firstNode.text!,
fontFamily: 'KaTeX_Main',
- fontSize: fontSize,
- fontHeight: kBaseKatexTextStyle.height!);
+ fontSize: baseTextStyle.fontSize!,
+ fontHeight: baseTextStyle.height!);
nodes = nodes.skip(1);
for (var katexNode in nodes) {
@@ -656,8 +666,8 @@ void main() {
final fontFamily = katexNode.styles.fontFamily!;
checkKatexText(tester, katexNode.text!,
fontFamily: fontFamily,
- fontSize: fontSize,
- fontHeight: kBaseKatexTextStyle.height!);
+ fontSize: baseTextStyle.fontSize!,
+ fontHeight: baseTextStyle.height!);
}
});
});
From 8e12ca9fb7d1c7d3af05076c6dc7c1c2c94ac6d4 Mon Sep 17 00:00:00 2001
From: Rajesh Malviya
Date: Tue, 1 Apr 2025 18:32:25 +0530
Subject: [PATCH 05/13] content: Support parsing and handling inline styles for
KaTeX content
---
lib/model/katex.dart | 86 +++++++++++++++++++++++++++++++++++-
lib/widgets/content.dart | 20 ++++++++-
test/model/content_test.dart | 24 +++++-----
3 files changed, 116 insertions(+), 14 deletions(-)
diff --git a/lib/model/katex.dart b/lib/model/katex.dart
index f6375f907e..8ef4e5e955 100644
--- a/lib/model/katex.dart
+++ b/lib/model/katex.dart
@@ -1,3 +1,5 @@
+import 'package:csslib/parser.dart' as css_parser;
+import 'package:csslib/visitor.dart' as css_visitor;
import 'package:flutter/foundation.dart';
import 'package:html/dom.dart' as dom;
@@ -350,11 +352,70 @@ class _KatexParser {
}
if (text == null && spans == null) throw KatexHtmlParseError();
+ final inlineStyles = _parseSpanInlineStyles(element);
+
return KatexNode(
- styles: styles,
+ styles: inlineStyles != null
+ ? styles.merge(inlineStyles)
+ : styles,
text: text,
nodes: spans);
}
+
+ KatexSpanStyles? _parseSpanInlineStyles(dom.Element element) {
+ if (element.attributes case {'style': final styleStr}) {
+ // `package:csslib` doesn't seem to have a way to parse inline styles:
+ // https://github.com/dart-lang/tools/issues/1173
+ // So, workaround that by wrapping it in a universal declaration.
+ final stylesheet = css_parser.parse('*{$styleStr}');
+ if (stylesheet.topLevels case [css_visitor.RuleSet() && final rule]) {
+ double? heightEm;
+ double? verticalAlignEm;
+
+ for (final declaration in rule.declarationGroup.declarations) {
+ if (declaration case css_visitor.Declaration(
+ :final property,
+ expression: css_visitor.Expressions(
+ expressions: [css_visitor.Expression() && final expression]),
+ )) {
+ switch (property) {
+ case 'height':
+ heightEm = _getEm(expression);
+ if (heightEm != null) continue;
+
+ case 'vertical-align':
+ verticalAlignEm = _getEm(expression);
+ if (verticalAlignEm != null) continue;
+ }
+
+ // TODO handle more CSS properties
+ assert(debugLog('KaTeX: Unsupported CSS expression:'
+ ' ${expression.toDebugString()}'));
+ _hasError = true;
+ } else {
+ throw KatexHtmlParseError();
+ }
+ }
+
+ return KatexSpanStyles(
+ heightEm: heightEm,
+ verticalAlignEm: verticalAlignEm,
+ );
+ } else {
+ throw KatexHtmlParseError();
+ }
+ }
+ return null;
+ }
+
+ /// Returns the CSS `em` unit value if the given [expression] is actually an
+ /// `em` unit expression, else returns null.
+ double? _getEm(css_visitor.Expression expression) {
+ if (expression is css_visitor.EmTerm && expression.value is num) {
+ return (expression.value as num).toDouble();
+ }
+ return null;
+ }
}
enum KatexSpanFontWeight {
@@ -374,6 +435,9 @@ enum KatexSpanTextAlign {
@immutable
class KatexSpanStyles {
+ final double? heightEm;
+ final double? verticalAlignEm;
+
final String? fontFamily;
final double? fontSizeEm;
final KatexSpanFontWeight? fontWeight;
@@ -381,6 +445,8 @@ class KatexSpanStyles {
final KatexSpanTextAlign? textAlign;
const KatexSpanStyles({
+ this.heightEm,
+ this.verticalAlignEm,
this.fontFamily,
this.fontSizeEm,
this.fontWeight,
@@ -391,6 +457,8 @@ class KatexSpanStyles {
@override
int get hashCode => Object.hash(
'KatexSpanStyles',
+ heightEm,
+ verticalAlignEm,
fontFamily,
fontSizeEm,
fontWeight,
@@ -401,6 +469,8 @@ class KatexSpanStyles {
@override
bool operator ==(Object other) {
return other is KatexSpanStyles &&
+ other.heightEm == heightEm &&
+ other.verticalAlignEm == verticalAlignEm &&
other.fontFamily == fontFamily &&
other.fontSizeEm == fontSizeEm &&
other.fontWeight == fontWeight &&
@@ -411,6 +481,8 @@ class KatexSpanStyles {
@override
String toString() {
final args = [];
+ if (heightEm != null) args.add('heightEm: $heightEm');
+ if (verticalAlignEm != null) args.add('verticalAlignEm: $verticalAlignEm');
if (fontFamily != null) args.add('fontFamily: $fontFamily');
if (fontSizeEm != null) args.add('fontSizeEm: $fontSizeEm');
if (fontWeight != null) args.add('fontWeight: $fontWeight');
@@ -418,6 +490,18 @@ class KatexSpanStyles {
if (textAlign != null) args.add('textAlign: $textAlign');
return '${objectRuntimeType(this, 'KatexSpanStyles')}(${args.join(', ')})';
}
+
+ KatexSpanStyles merge(KatexSpanStyles other) {
+ return KatexSpanStyles(
+ heightEm: other.heightEm ?? heightEm,
+ verticalAlignEm: other.verticalAlignEm ?? verticalAlignEm,
+ fontFamily: other.fontFamily ?? fontFamily,
+ fontSizeEm: other.fontSizeEm ?? fontSizeEm,
+ fontStyle: other.fontStyle ?? fontStyle,
+ fontWeight: other.fontWeight ?? fontWeight,
+ textAlign: other.textAlign ?? textAlign,
+ );
+ }
}
class KatexHtmlParseError extends Error {
diff --git a/lib/widgets/content.dart b/lib/widgets/content.dart
index bb60fc9be6..d5fd600658 100644
--- a/lib/widgets/content.dart
+++ b/lib/widgets/content.dart
@@ -895,7 +895,9 @@ class _KatexSpan extends StatelessWidget {
Widget build(BuildContext context) {
final em = DefaultTextStyle.of(context).style.fontSize!;
- Widget widget = const SizedBox.shrink();
+ const defaultWidget = SizedBox.shrink();
+
+ Widget widget = defaultWidget;
if (node.text != null) {
widget = Text(node.text!);
} else if (node.nodes != null && node.nodes!.isNotEmpty) {
@@ -952,7 +954,21 @@ class _KatexSpan extends StatelessWidget {
textAlign: textAlign,
child: widget);
}
- return widget;
+
+ if (styles.verticalAlignEm != null && styles.heightEm != null) {
+ assert(widget == defaultWidget);
+ widget = Baseline(
+ baseline: (styles.verticalAlignEm! + styles.heightEm!) * em,
+ baselineType: TextBaseline.alphabetic,
+ child: const Text(''));
+ }
+
+ return SizedBox(
+ height: styles.heightEm != null
+ ? styles.heightEm! * em
+ : null,
+ child: widget,
+ );
}
}
diff --git a/test/model/content_test.dart b/test/model/content_test.dart
index 6a0bc6ebe7..f8c1fc7c6d 100644
--- a/test/model/content_test.dart
+++ b/test/model/content_test.dart
@@ -519,7 +519,7 @@ class ContentExample {
'λ
',
MathInlineNode(texSource: r'\lambda', nodes: [
KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
- KatexNode(styles: KatexSpanStyles(), text: null, nodes: []),
+ KatexNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
KatexNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
@@ -539,7 +539,7 @@ class ContentExample {
'λ
',
[MathBlockNode(texSource: r'\lambda', nodes: [
KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
- KatexNode(styles: KatexSpanStyles(), text: null, nodes: []),
+ KatexNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
KatexNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
@@ -564,7 +564,7 @@ class ContentExample {
'b', [
MathBlockNode(texSource: 'a', nodes: [
KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
- KatexNode(styles: KatexSpanStyles(), text: null, nodes: []),
+ KatexNode(styles: KatexSpanStyles(heightEm: 0.4306), text: null, nodes: []),
KatexNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
@@ -575,7 +575,7 @@ class ContentExample {
]),
MathBlockNode(texSource: 'b', nodes: [
KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
- KatexNode(styles: KatexSpanStyles(), text: null, nodes: []),
+ KatexNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
KatexNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
@@ -603,7 +603,7 @@ class ContentExample {
[QuotationNode([
MathBlockNode(texSource: r'\lambda', nodes: [
KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
- KatexNode(styles: KatexSpanStyles(), text: null, nodes: []),
+ KatexNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
KatexNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
@@ -632,7 +632,7 @@ class ContentExample {
[QuotationNode([
MathBlockNode(texSource: 'a', nodes: [
KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
- KatexNode(styles: KatexSpanStyles(), text: null, nodes: []),
+ KatexNode(styles: KatexSpanStyles(heightEm: 0.4306), text: null, nodes: []),
KatexNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
@@ -643,7 +643,7 @@ class ContentExample {
]),
MathBlockNode(texSource: 'b', nodes: [
KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
- KatexNode(styles: KatexSpanStyles(), text: null, nodes: []),
+ KatexNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
KatexNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
@@ -681,7 +681,7 @@ class ContentExample {
]),
MathBlockNode(texSource: 'a', nodes: [
KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
- KatexNode(styles: KatexSpanStyles(),text: null, nodes: []),
+ KatexNode(styles: KatexSpanStyles(heightEm: 0.4306),text: null, nodes: []),
KatexNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
@@ -732,7 +732,7 @@ class ContentExample {
text: null,
nodes: [
KatexNode(
- styles: KatexSpanStyles(),
+ styles: KatexSpanStyles(heightEm: 1.6034),
text: null,
nodes: []),
KatexNode(
@@ -801,7 +801,7 @@ class ContentExample {
text: null,
nodes: [
KatexNode(
- styles: KatexSpanStyles(),
+ styles: KatexSpanStyles(heightEm: 1.6034),
text: null,
nodes: []),
KatexNode(
@@ -846,7 +846,9 @@ class ContentExample {
text: null,
nodes: [
KatexNode(
- styles: KatexSpanStyles(),
+ styles: KatexSpanStyles(
+ heightEm: 3.0,
+ verticalAlignEm: -1.25),
text: null,
nodes: []),
KatexNode(
From c4393967f7ebdc4cbe5145ab8524201e90fda4c7 Mon Sep 17 00:00:00 2001
From: Rajesh Malviya
Date: Thu, 24 Apr 2025 13:21:18 +0530
Subject: [PATCH 06/13] content [nfc]: Reintroduce KatexNode as a base sealed
class
And rename previous type to KatexSpanNode, also while making it a
subtype of KatexNode.
---
lib/model/content.dart | 8 ++-
lib/model/katex.dart | 2 +-
lib/widgets/content.dart | 6 +-
test/model/content_test.dart | 104 ++++++++++++++++-----------------
test/widgets/content_test.dart | 12 ++--
5 files changed, 70 insertions(+), 62 deletions(-)
diff --git a/lib/model/content.dart b/lib/model/content.dart
index 59f7b41aad..768031ae9a 100644
--- a/lib/model/content.dart
+++ b/lib/model/content.dart
@@ -369,8 +369,12 @@ abstract class MathNode extends ContentNode {
}
}
-class KatexNode extends ContentNode {
- const KatexNode({
+sealed class KatexNode extends ContentNode {
+ const KatexNode({super.debugHtmlNode});
+}
+
+class KatexSpanNode extends KatexNode {
+ const KatexSpanNode({
required this.styles,
required this.text,
required this.nodes,
diff --git a/lib/model/katex.dart b/lib/model/katex.dart
index 8ef4e5e955..9275fd921d 100644
--- a/lib/model/katex.dart
+++ b/lib/model/katex.dart
@@ -354,7 +354,7 @@ class _KatexParser {
final inlineStyles = _parseSpanInlineStyles(element);
- return KatexNode(
+ return KatexSpanNode(
styles: inlineStyles != null
? styles.merge(inlineStyles)
: styles,
diff --git a/lib/widgets/content.dart b/lib/widgets/content.dart
index d5fd600658..9cf51482c9 100644
--- a/lib/widgets/content.dart
+++ b/lib/widgets/content.dart
@@ -881,7 +881,9 @@ class _KatexNodeList extends StatelessWidget {
return WidgetSpan(
alignment: PlaceholderAlignment.baseline,
baseline: TextBaseline.alphabetic,
- child: _KatexSpan(e));
+ child: switch (e) {
+ KatexSpanNode() => _KatexSpan(e),
+ });
}))));
}
}
@@ -889,7 +891,7 @@ class _KatexNodeList extends StatelessWidget {
class _KatexSpan extends StatelessWidget {
const _KatexSpan(this.node);
- final KatexNode node;
+ final KatexSpanNode node;
@override
Widget build(BuildContext context) {
diff --git a/test/model/content_test.dart b/test/model/content_test.dart
index f8c1fc7c6d..315baa5e4c 100644
--- a/test/model/content_test.dart
+++ b/test/model/content_test.dart
@@ -518,9 +518,9 @@ class ContentExample {
' \\lambda '
'λ',
MathInlineNode(texSource: r'\lambda', nodes: [
- KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
- KatexNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
- KatexNode(
+ KatexSpanNode(styles: KatexSpanStyles(), text: null, nodes: [
+ KatexSpanNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
+ KatexSpanNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
fontStyle: KatexSpanFontStyle.italic),
@@ -538,9 +538,9 @@ class ContentExample {
'\\lambda'
'λ',
[MathBlockNode(texSource: r'\lambda', nodes: [
- KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
- KatexNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
- KatexNode(
+ KatexSpanNode(styles: KatexSpanStyles(), text: null, nodes: [
+ KatexSpanNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
+ KatexSpanNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
fontStyle: KatexSpanFontStyle.italic),
@@ -563,9 +563,9 @@ class ContentExample {
'b'
'b', [
MathBlockNode(texSource: 'a', nodes: [
- KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
- KatexNode(styles: KatexSpanStyles(heightEm: 0.4306), text: null, nodes: []),
- KatexNode(
+ KatexSpanNode(styles: KatexSpanStyles(), text: null, nodes: [
+ KatexSpanNode(styles: KatexSpanStyles(heightEm: 0.4306), text: null, nodes: []),
+ KatexSpanNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
fontStyle: KatexSpanFontStyle.italic),
@@ -574,9 +574,9 @@ class ContentExample {
]),
]),
MathBlockNode(texSource: 'b', nodes: [
- KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
- KatexNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
- KatexNode(
+ KatexSpanNode(styles: KatexSpanStyles(), text: null, nodes: [
+ KatexSpanNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
+ KatexSpanNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
fontStyle: KatexSpanFontStyle.italic),
@@ -602,9 +602,9 @@ class ContentExample {
'
\n\n',
[QuotationNode([
MathBlockNode(texSource: r'\lambda', nodes: [
- KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
- KatexNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
- KatexNode(
+ KatexSpanNode(styles: KatexSpanStyles(), text: null, nodes: [
+ KatexSpanNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
+ KatexSpanNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
fontStyle: KatexSpanFontStyle.italic),
@@ -631,9 +631,9 @@ class ContentExample {
'
\n\n',
[QuotationNode([
MathBlockNode(texSource: 'a', nodes: [
- KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
- KatexNode(styles: KatexSpanStyles(heightEm: 0.4306), text: null, nodes: []),
- KatexNode(
+ KatexSpanNode(styles: KatexSpanStyles(), text: null, nodes: [
+ KatexSpanNode(styles: KatexSpanStyles(heightEm: 0.4306), text: null, nodes: []),
+ KatexSpanNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
fontStyle: KatexSpanFontStyle.italic),
@@ -642,9 +642,9 @@ class ContentExample {
]),
]),
MathBlockNode(texSource: 'b', nodes: [
- KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
- KatexNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
- KatexNode(
+ KatexSpanNode(styles: KatexSpanStyles(), text: null, nodes: [
+ KatexSpanNode(styles: KatexSpanStyles(heightEm: 0.6944), text: null, nodes: []),
+ KatexSpanNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
fontStyle: KatexSpanFontStyle.italic),
@@ -680,9 +680,9 @@ class ContentExample {
originalHeight: null),
]),
MathBlockNode(texSource: 'a', nodes: [
- KatexNode(styles: KatexSpanStyles(), text: null, nodes: [
- KatexNode(styles: KatexSpanStyles(heightEm: 0.4306),text: null, nodes: []),
- KatexNode(
+ KatexSpanNode(styles: KatexSpanStyles(), text: null, nodes: [
+ KatexSpanNode(styles: KatexSpanStyles(heightEm: 0.4306),text: null, nodes: []),
+ KatexSpanNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
fontStyle: KatexSpanFontStyle.italic),
@@ -727,51 +727,51 @@ class ContentExample {
MathBlockNode(
texSource: "\\Huge 1\n\\huge 2\n\\LARGE 3\n\\Large 4\n\\large 5\n\\normalsize 6\n\\small 7\n\\footnotesize 8\n\\scriptsize 9\n\\tiny 0",
nodes: [
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(),
text: null,
nodes: [
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(heightEm: 1.6034),
text: null,
nodes: []),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(fontSizeEm: 2.488), // .reset-size6.size11
text: '1',
nodes: null),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(fontSizeEm: 2.074), // .reset-size6.size10
text: '2',
nodes: null),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(fontSizeEm: 1.728), // .reset-size6.size9
text: '3',
nodes: null),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(fontSizeEm: 1.44), // .reset-size6.size8
text: '4',
nodes: null),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(fontSizeEm: 1.2), // .reset-size6.size7
text: '5',
nodes: null),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(fontSizeEm: 1.0), // .reset-size6.size6
text: '6',
nodes: null),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(fontSizeEm: 0.9), // .reset-size6.size5
text: '7',
nodes: null),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(fontSizeEm: 0.8), // .reset-size6.size4
text: '8',
nodes: null),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(fontSizeEm: 0.7), // .reset-size6.size3
text: '9',
nodes: null),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(fontSizeEm: 0.5), // .reset-size6.size1
text: '0',
nodes: null),
@@ -796,23 +796,23 @@ class ContentExample {
MathBlockNode(
texSource: '\\tiny {1 \\Huge 2}',
nodes: [
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(),
text: null,
nodes: [
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(heightEm: 1.6034),
text: null,
nodes: []),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(fontSizeEm: 0.5), // reset-size6 size1
text: null,
nodes: [
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(),
text: '1',
nodes: null),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(fontSizeEm: 4.976), // reset-size1 size11
text: '2',
nodes: null),
@@ -841,52 +841,52 @@ class ContentExample {
MathBlockNode(
texSource: '⟨ \\big( \\Big[ \\bigg⌈ \\Bigg⌊',
nodes: [
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(),
text: null,
nodes: [
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(
heightEm: 3.0,
verticalAlignEm: -1.25),
text: null,
nodes: []),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(),
text: '⟨',
nodes: null),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(),
text: null,
nodes: [
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(fontFamily: 'KaTeX_Size1'),
text: '(',
nodes: null),
]),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(),
text: null,
nodes: [
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(fontFamily: 'KaTeX_Size2'),
text: '[',
nodes: null),
]),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(),
text: null,
nodes: [
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(fontFamily: 'KaTeX_Size3'),
text: '⌈',
nodes: null),
]),
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(),
text: null,
nodes: [
- KatexNode(
+ KatexSpanNode(
styles: KatexSpanStyles(fontFamily: 'KaTeX_Size4'),
text: '⌊',
nodes: null),
diff --git a/test/widgets/content_test.dart b/test/widgets/content_test.dart
index 84c2b15ad2..d5445fb931 100644
--- a/test/widgets/content_test.dart
+++ b/test/widgets/content_test.dart
@@ -600,9 +600,10 @@ void main() {
mkBaseKatexTextStyle(ContentTheme.of(context).textStylePlainParagraph);
final mathBlockNode = content.expectedNodes.single as MathBlockNode;
- final baseNode = mathBlockNode.nodes!.single;
+ final baseNode = mathBlockNode.nodes!.single as KatexSpanNode;
final nodes = baseNode.nodes!.skip(1); // Skip .strut node.
- for (final katexNode in nodes) {
+ for (var katexNode in nodes) {
+ katexNode = katexNode as KatexSpanNode;
final fontSize = katexNode.styles.fontSizeEm! * baseTextStyle.fontSize!;
checkKatexText(tester, katexNode.text!,
fontFamily: 'KaTeX_Main',
@@ -647,14 +648,14 @@ void main() {
await prepareContent(tester, plainContent(content.html));
final mathBlockNode = content.expectedNodes.single as MathBlockNode;
- final baseNode = mathBlockNode.nodes!.single;
+ final baseNode = mathBlockNode.nodes!.single as KatexSpanNode;
var nodes = baseNode.nodes!.skip(1); // Skip .strut node.
final context = tester.element(find.byType(MathBlock));
final baseTextStyle =
mkBaseKatexTextStyle(ContentTheme.of(context).textStylePlainParagraph);
- final firstNode = nodes.first;
+ final firstNode = nodes.first as KatexSpanNode;
checkKatexText(tester, firstNode.text!,
fontFamily: 'KaTeX_Main',
fontSize: baseTextStyle.fontSize!,
@@ -662,7 +663,8 @@ void main() {
nodes = nodes.skip(1);
for (var katexNode in nodes) {
- katexNode = katexNode.nodes!.single; // Skip empty .mord parent.
+ katexNode = katexNode as KatexSpanNode;
+ katexNode = katexNode.nodes!.single as KatexSpanNode; // Skip empty .mord parent.
final fontFamily = katexNode.styles.fontFamily!;
checkKatexText(tester, katexNode.text!,
fontFamily: fontFamily,
From 726621a86596599344e7ce440bd79a2caa5bcdb1 Mon Sep 17 00:00:00 2001
From: Rajesh Malviya
Date: Mon, 19 May 2025 21:36:45 +0530
Subject: [PATCH 07/13] content: Ignore more KaTeX classes that don't have CSS
definition
---
lib/model/katex.dart | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/lib/model/katex.dart b/lib/model/katex.dart
index 9275fd921d..aaaa7831c8 100644
--- a/lib/model/katex.dart
+++ b/lib/model/katex.dart
@@ -326,6 +326,12 @@ class _KatexParser {
case 'mord':
case 'mopen':
+ case 'mtight':
+ case 'text':
+ case 'mrel':
+ case 'mop':
+ case 'mclose':
+ case 'minner':
// Ignore these classes because they don't have a CSS definition
// in katex.scss, but we encounter them in the generated HTML.
break;
From 284464df77fe0f3de98ba94c8414e287f1f839a5 Mon Sep 17 00:00:00 2001
From: Rajesh Malviya
Date: Mon, 19 May 2025 21:42:55 +0530
Subject: [PATCH 08/13] content: Handle 'mspace' and 'msupsub' KaTeX CSS
classes
---
lib/model/katex.dart | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/lib/model/katex.dart b/lib/model/katex.dart
index aaaa7831c8..ba9e379849 100644
--- a/lib/model/katex.dart
+++ b/lib/model/katex.dart
@@ -272,6 +272,21 @@ class _KatexParser {
fontStyle = KatexSpanFontStyle.normal;
// TODO handle skipped class declarations between .mainrm and
+ // .mspace .
+
+ case 'mspace':
+ // .mspace { ... }
+ // Do nothing, it has properties that don't need special handling.
+ break;
+
+ // TODO handle skipped class declarations between .mspace and
+ // .thinbox .
+
+ case 'msupsub':
+ // .msupsub { text-align: left; }
+ textAlign = KatexSpanTextAlign.left;
+
+ // TODO handle skipped class declarations between .msupsub and
// .sizing .
case 'sizing':
From ee9d42cf4df64df0e862338bea7c68dc9447976f Mon Sep 17 00:00:00 2001
From: Rajesh Malviya
Date: Mon, 19 May 2025 21:46:15 +0530
Subject: [PATCH 09/13] content: Handle positive margin-right and margin-left
in KaTeX spans
---
lib/model/katex.dart | 31 +++++++++++++++++++++++++++++++
lib/widgets/content.dart | 25 ++++++++++++++++++++++++-
2 files changed, 55 insertions(+), 1 deletion(-)
diff --git a/lib/model/katex.dart b/lib/model/katex.dart
index ba9e379849..8509e2efc7 100644
--- a/lib/model/katex.dart
+++ b/lib/model/katex.dart
@@ -392,6 +392,8 @@ class _KatexParser {
if (stylesheet.topLevels case [css_visitor.RuleSet() && final rule]) {
double? heightEm;
double? verticalAlignEm;
+ double? marginRightEm;
+ double? marginLeftEm;
for (final declaration in rule.declarationGroup.declarations) {
if (declaration case css_visitor.Declaration(
@@ -407,6 +409,20 @@ class _KatexParser {
case 'vertical-align':
verticalAlignEm = _getEm(expression);
if (verticalAlignEm != null) continue;
+
+ case 'margin-right':
+ marginRightEm = _getEm(expression);
+ if (marginRightEm != null) {
+ if (marginRightEm < 0) throw KatexHtmlParseError();
+ continue;
+ }
+
+ case 'margin-left':
+ marginLeftEm = _getEm(expression);
+ if (marginLeftEm != null) {
+ if (marginLeftEm < 0) throw KatexHtmlParseError();
+ continue;
+ }
}
// TODO handle more CSS properties
@@ -421,6 +437,8 @@ class _KatexParser {
return KatexSpanStyles(
heightEm: heightEm,
verticalAlignEm: verticalAlignEm,
+ marginRightEm: marginRightEm,
+ marginLeftEm: marginLeftEm,
);
} else {
throw KatexHtmlParseError();
@@ -459,6 +477,9 @@ class KatexSpanStyles {
final double? heightEm;
final double? verticalAlignEm;
+ final double? marginRightEm;
+ final double? marginLeftEm;
+
final String? fontFamily;
final double? fontSizeEm;
final KatexSpanFontWeight? fontWeight;
@@ -468,6 +489,8 @@ class KatexSpanStyles {
const KatexSpanStyles({
this.heightEm,
this.verticalAlignEm,
+ this.marginRightEm,
+ this.marginLeftEm,
this.fontFamily,
this.fontSizeEm,
this.fontWeight,
@@ -480,6 +503,8 @@ class KatexSpanStyles {
'KatexSpanStyles',
heightEm,
verticalAlignEm,
+ marginRightEm,
+ marginLeftEm,
fontFamily,
fontSizeEm,
fontWeight,
@@ -492,6 +517,8 @@ class KatexSpanStyles {
return other is KatexSpanStyles &&
other.heightEm == heightEm &&
other.verticalAlignEm == verticalAlignEm &&
+ other.marginRightEm == marginRightEm &&
+ other.marginLeftEm == marginLeftEm &&
other.fontFamily == fontFamily &&
other.fontSizeEm == fontSizeEm &&
other.fontWeight == fontWeight &&
@@ -504,6 +531,8 @@ class KatexSpanStyles {
final args = [];
if (heightEm != null) args.add('heightEm: $heightEm');
if (verticalAlignEm != null) args.add('verticalAlignEm: $verticalAlignEm');
+ if (marginRightEm != null) args.add('marginRightEm: $marginRightEm');
+ if (marginLeftEm != null) args.add('marginLeftEm: $marginLeftEm');
if (fontFamily != null) args.add('fontFamily: $fontFamily');
if (fontSizeEm != null) args.add('fontSizeEm: $fontSizeEm');
if (fontWeight != null) args.add('fontWeight: $fontWeight');
@@ -516,6 +545,8 @@ class KatexSpanStyles {
return KatexSpanStyles(
heightEm: other.heightEm ?? heightEm,
verticalAlignEm: other.verticalAlignEm ?? verticalAlignEm,
+ marginRightEm: other.marginRightEm ?? marginRightEm,
+ marginLeftEm: other.marginLeftEm ?? marginLeftEm,
fontFamily: other.fontFamily ?? fontFamily,
fontSizeEm: other.fontSizeEm ?? fontSizeEm,
fontStyle: other.fontStyle ?? fontStyle,
diff --git a/lib/widgets/content.dart b/lib/widgets/content.dart
index 9cf51482c9..570bb7d295 100644
--- a/lib/widgets/content.dart
+++ b/lib/widgets/content.dart
@@ -965,7 +965,30 @@ class _KatexSpan extends StatelessWidget {
child: const Text(''));
}
- return SizedBox(
+ final marginRight = switch (styles.marginRightEm) {
+ double marginRightEm => marginRightEm * em,
+ null => null,
+ };
+ final marginLeft = switch (styles.marginLeftEm) {
+ double marginLeftEm => marginLeftEm * em,
+ null => null,
+ };
+
+ EdgeInsets? margin;
+ if (marginRight != null || marginLeft != null) {
+ margin = EdgeInsets.zero;
+ if (marginRight != null) {
+ assert(marginRight >= 0);
+ margin += EdgeInsets.only(right: marginRight);
+ }
+ if (marginLeft != null) {
+ assert(marginLeft >= 0);
+ margin += EdgeInsets.only(left: marginLeft);
+ }
+ }
+
+ return Container(
+ margin: margin,
height: styles.heightEm != null
? styles.heightEm! * em
: null,
From 002b33833c52e5bd940b03932e2602fbd77e39a6 Mon Sep 17 00:00:00 2001
From: Rajesh Malviya
Date: Mon, 19 May 2025 21:54:24 +0530
Subject: [PATCH 10/13] content: Handle 'top' property in KaTeX span inline
style
---
lib/model/katex.dart | 13 +++++++++++++
lib/widgets/content.dart | 3 +++
2 files changed, 16 insertions(+)
diff --git a/lib/model/katex.dart b/lib/model/katex.dart
index 8509e2efc7..f99052b889 100644
--- a/lib/model/katex.dart
+++ b/lib/model/katex.dart
@@ -392,6 +392,7 @@ class _KatexParser {
if (stylesheet.topLevels case [css_visitor.RuleSet() && final rule]) {
double? heightEm;
double? verticalAlignEm;
+ double? topEm;
double? marginRightEm;
double? marginLeftEm;
@@ -410,6 +411,10 @@ class _KatexParser {
verticalAlignEm = _getEm(expression);
if (verticalAlignEm != null) continue;
+ case 'top':
+ topEm = _getEm(expression);
+ if (topEm != null) continue;
+
case 'margin-right':
marginRightEm = _getEm(expression);
if (marginRightEm != null) {
@@ -436,6 +441,7 @@ class _KatexParser {
return KatexSpanStyles(
heightEm: heightEm,
+ topEm: topEm,
verticalAlignEm: verticalAlignEm,
marginRightEm: marginRightEm,
marginLeftEm: marginLeftEm,
@@ -477,6 +483,8 @@ class KatexSpanStyles {
final double? heightEm;
final double? verticalAlignEm;
+ final double? topEm;
+
final double? marginRightEm;
final double? marginLeftEm;
@@ -489,6 +497,7 @@ class KatexSpanStyles {
const KatexSpanStyles({
this.heightEm,
this.verticalAlignEm,
+ this.topEm,
this.marginRightEm,
this.marginLeftEm,
this.fontFamily,
@@ -503,6 +512,7 @@ class KatexSpanStyles {
'KatexSpanStyles',
heightEm,
verticalAlignEm,
+ topEm,
marginRightEm,
marginLeftEm,
fontFamily,
@@ -517,6 +527,7 @@ class KatexSpanStyles {
return other is KatexSpanStyles &&
other.heightEm == heightEm &&
other.verticalAlignEm == verticalAlignEm &&
+ other.topEm == topEm &&
other.marginRightEm == marginRightEm &&
other.marginLeftEm == marginLeftEm &&
other.fontFamily == fontFamily &&
@@ -531,6 +542,7 @@ class KatexSpanStyles {
final args = [];
if (heightEm != null) args.add('heightEm: $heightEm');
if (verticalAlignEm != null) args.add('verticalAlignEm: $verticalAlignEm');
+ if (topEm != null) args.add('topEm: $topEm');
if (marginRightEm != null) args.add('marginRightEm: $marginRightEm');
if (marginLeftEm != null) args.add('marginLeftEm: $marginLeftEm');
if (fontFamily != null) args.add('fontFamily: $fontFamily');
@@ -545,6 +557,7 @@ class KatexSpanStyles {
return KatexSpanStyles(
heightEm: other.heightEm ?? heightEm,
verticalAlignEm: other.verticalAlignEm ?? verticalAlignEm,
+ topEm: other.topEm ?? topEm,
marginRightEm: other.marginRightEm ?? marginRightEm,
marginLeftEm: other.marginLeftEm ?? marginLeftEm,
fontFamily: other.fontFamily ?? fontFamily,
diff --git a/lib/widgets/content.dart b/lib/widgets/content.dart
index 570bb7d295..57bed7ab4d 100644
--- a/lib/widgets/content.dart
+++ b/lib/widgets/content.dart
@@ -992,6 +992,9 @@ class _KatexSpan extends StatelessWidget {
height: styles.heightEm != null
? styles.heightEm! * em
: null,
+ transform: styles.topEm != null
+ ? Matrix4.translationValues(0, styles.topEm! * em, 0)
+ : null,
child: widget,
);
}
From 7588b96af7d45ad523f9105800edef73964960e9 Mon Sep 17 00:00:00 2001
From: Rajesh Malviya
Date: Mon, 19 May 2025 22:04:00 +0530
Subject: [PATCH 11/13] content [nfc]: Remove the `inline` property in _Katex
widget
And inline the behaviour for `inline: false` in MathBlock widget.
---
lib/widgets/content.dart | 21 +++++++--------------
1 file changed, 7 insertions(+), 14 deletions(-)
diff --git a/lib/widgets/content.dart b/lib/widgets/content.dart
index 57bed7ab4d..5611f351f4 100644
--- a/lib/widgets/content.dart
+++ b/lib/widgets/content.dart
@@ -817,10 +817,12 @@ class MathBlock extends StatelessWidget {
children: [TextSpan(text: node.texSource)])));
}
- return _Katex(
- inline: false,
- textStyle: ContentTheme.of(context).textStylePlainParagraph,
- nodes: nodes);
+ return Center(
+ child: SingleChildScrollViewWithScrollbar(
+ scrollDirection: Axis.horizontal,
+ child: _Katex(
+ textStyle: ContentTheme.of(context).textStylePlainParagraph,
+ nodes: nodes)));
}
}
@@ -841,12 +843,10 @@ TextStyle mkBaseKatexTextStyle(TextStyle style) {
class _Katex extends StatelessWidget {
const _Katex({
- required this.inline,
required this.textStyle,
required this.nodes,
});
- final bool inline;
final TextStyle textStyle;
final List nodes;
@@ -854,13 +854,6 @@ class _Katex extends StatelessWidget {
Widget build(BuildContext context) {
Widget widget = _KatexNodeList(nodes: nodes);
- if (!inline) {
- widget = Center(
- child: SingleChildScrollViewWithScrollbar(
- scrollDirection: Axis.horizontal,
- child: widget));
- }
-
return Directionality(
textDirection: TextDirection.ltr,
child: DefaultTextStyle(
@@ -1318,7 +1311,7 @@ class _InlineContentBuilder {
: WidgetSpan(
alignment: PlaceholderAlignment.baseline,
baseline: TextBaseline.alphabetic,
- child: _Katex(inline: true, textStyle: widget.style, nodes: nodes));
+ child: _Katex(textStyle: widget.style, nodes: nodes));
case GlobalTimeNode():
return WidgetSpan(alignment: PlaceholderAlignment.middle,
From d01d5d47aeb7a2961cb6555dca6d2b3fb0ddca80 Mon Sep 17 00:00:00 2001
From: Rajesh Malviya
Date: Mon, 19 May 2025 22:33:19 +0530
Subject: [PATCH 12/13] deps: Add file as a direct dependency
It will be used to load fonts for golden tests.
---
pubspec.lock | 2 +-
pubspec.yaml | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/pubspec.lock b/pubspec.lock
index 4784adf19a..7491911046 100644
--- a/pubspec.lock
+++ b/pubspec.lock
@@ -307,7 +307,7 @@ packages:
source: hosted
version: "2.1.4"
file:
- dependency: transitive
+ dependency: "direct dev"
description:
name: file
sha256: a3b4f84adafef897088c160faf7dfffb7696046cb13ae90b508c2cbc95d3b8d4
diff --git a/pubspec.yaml b/pubspec.yaml
index d9fa354b16..646156c377 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -97,6 +97,7 @@ dev_dependencies:
clock: ^1.1.1
drift_dev: ^2.5.2
fake_async: ^1.3.1
+ file: ^7.0.1
flutter_checks: ^0.1.2
flutter_lints: ^5.0.0
ini: ^2.1.0
From ec1430677cdec003913cc6d4e04894aca0b2a1ef Mon Sep 17 00:00:00 2001
From: Rajesh Malviya
Date: Tue, 1 Apr 2025 21:29:15 +0530
Subject: [PATCH 13/13] content: Handle vertical offset spans in KaTeX content
Implement handling most common types of `vlist` spans.
---
lib/model/content.dart | 30 +++
lib/model/katex.dart | 107 ++++++++-
lib/widgets/content.dart | 27 ++-
test/model/content_test.dart | 405 +++++++++++++++++++++++++++++++++
test/widgets/content_test.dart | 75 ++++++
5 files changed, 639 insertions(+), 5 deletions(-)
diff --git a/lib/model/content.dart b/lib/model/content.dart
index 768031ae9a..c3adb189ee 100644
--- a/lib/model/content.dart
+++ b/lib/model/content.dart
@@ -406,6 +406,36 @@ class KatexSpanNode extends KatexNode {
}
}
+class KatexVlistNode extends KatexNode {
+ const KatexVlistNode({
+ required this.rows,
+ super.debugHtmlNode,
+ });
+
+ final List rows;
+
+ @override
+ List debugDescribeChildren() {
+ return rows.map((row) => row.toDiagnosticsNode()).toList();
+ }
+}
+
+class KatexVlistRowNode extends ContentNode {
+ const KatexVlistRowNode({
+ required this.verticalOffsetEm,
+ required this.node,
+ });
+
+ final double verticalOffsetEm;
+ final KatexSpanNode node;
+
+ @override
+ void debugFillProperties(DiagnosticPropertiesBuilder properties) {
+ super.debugFillProperties(properties);
+ properties.add(DoubleProperty('verticalOffsetEm', verticalOffsetEm));
+ }
+}
+
class MathBlockNode extends MathNode implements BlockContentNode {
const MathBlockNode({
super.debugHtmlNode,
diff --git a/lib/model/katex.dart b/lib/model/katex.dart
index f99052b889..16481e392f 100644
--- a/lib/model/katex.dart
+++ b/lib/model/katex.dart
@@ -133,6 +133,83 @@ class _KatexParser {
KatexNode _parseSpan(dom.Element element) {
// TODO maybe check if the sequence of ancestors matter for spans.
+ if (element.className.startsWith('vlist')) {
+ if (element case dom.Element(
+ localName: 'span',
+ className: 'vlist-t' || 'vlist-t vlist-t2',
+ nodes: [...],
+ ) && final vlistT) {
+ if (vlistT.attributes.containsKey('style')) throw KatexHtmlParseError();
+
+ final hasTwoVlistR = vlistT.className == 'vlist-t vlist-t2';
+ if (!hasTwoVlistR && vlistT.nodes.length != 1) throw KatexHtmlParseError();
+
+ if (hasTwoVlistR) {
+ if (vlistT.nodes case [
+ _,
+ dom.Element(localName: 'span', className: 'vlist-r', nodes: [
+ dom.Element(localName: 'span', className: 'vlist', nodes: [
+ dom.Element(localName: 'span', className: '', nodes: []),
+ ]),
+ ]),
+ ]) {
+ // Do nothing.
+ } else {
+ throw KatexHtmlParseError();
+ }
+ }
+
+ if (vlistT.nodes.first
+ case dom.Element(localName: 'span', className: 'vlist-r') &&
+ final vlistR) {
+ if (vlistR.attributes.containsKey('style')) throw KatexHtmlParseError();
+
+ if (vlistR.nodes.first
+ case dom.Element(localName: 'span', className: 'vlist') &&
+ final vlist) {
+ final rows = [];
+
+ for (final innerSpan in vlist.nodes) {
+ if (innerSpan case dom.Element(
+ localName: 'span',
+ className: '',
+ nodes: [
+ dom.Element(localName: 'span', className: 'pstrut') &&
+ final pstrutSpan,
+ ...final otherSpans,
+ ],
+ )) {
+ var styles = _parseSpanInlineStyles(innerSpan)!;
+ final topEm = styles.topEm ?? 0;
+
+ styles = styles.filter(topEm: false);
+
+ final pstrutStyles = _parseSpanInlineStyles(pstrutSpan)!;
+ final pstrutHeight = pstrutStyles.heightEm ?? 0;
+
+ rows.add(KatexVlistRowNode(
+ verticalOffsetEm: topEm + pstrutHeight,
+ node: KatexSpanNode(
+ styles: styles,
+ text: null,
+ nodes: _parseChildSpans(otherSpans))));
+ } else {
+ throw KatexHtmlParseError();
+ }
+ }
+
+ return KatexVlistNode(rows: rows);
+ } else {
+ throw KatexHtmlParseError();
+ }
+ } else {
+ throw KatexHtmlParseError();
+ }
+ } else {
+ throw KatexHtmlParseError();
+ }
+ }
+
// Aggregate the CSS styles that apply, in the same order as the CSS
// classes specified for this span, mimicking the behaviour on web.
//
@@ -141,7 +218,9 @@ class _KatexParser {
// https://github.com/KaTeX/KaTeX/blob/2fe1941b/src/styles/katex.scss
// A copy of class definition (where possible) is accompanied in a comment
// with each case statement to keep track of updates.
- final spanClasses = List.unmodifiable(element.className.split(' '));
+ final spanClasses = element.className != ''
+ ? List.unmodifiable(element.className.split(' '))
+ : const [];
String? fontFamily;
double? fontSizeEm;
KatexSpanFontWeight? fontWeight;
@@ -567,6 +646,32 @@ class KatexSpanStyles {
textAlign: other.textAlign ?? textAlign,
);
}
+
+ KatexSpanStyles filter({
+ bool heightEm = true,
+ bool verticalAlignEm = true,
+ bool topEm = true,
+ bool marginRightEm = true,
+ bool marginLeftEm = true,
+ bool fontFamily = true,
+ bool fontSizeEm = true,
+ bool fontWeight = true,
+ bool fontStyle = true,
+ bool textAlign = true,
+ }) {
+ return KatexSpanStyles(
+ heightEm: heightEm ? this.heightEm : null,
+ verticalAlignEm: verticalAlignEm ? this.verticalAlignEm : null,
+ topEm: topEm ? this.topEm : null,
+ marginRightEm: marginRightEm ? this.marginRightEm : null,
+ marginLeftEm: marginLeftEm ? this.marginLeftEm : null,
+ fontFamily: fontFamily ? this.fontFamily : null,
+ fontSizeEm: fontSizeEm ? this.fontSizeEm : null,
+ fontWeight: fontWeight ? this.fontWeight : null,
+ fontStyle: fontStyle ? this.fontStyle : null,
+ textAlign: textAlign ? this.textAlign : null,
+ );
+ }
}
class KatexHtmlParseError extends Error {
diff --git a/lib/widgets/content.dart b/lib/widgets/content.dart
index 5611f351f4..4d9ae5d688 100644
--- a/lib/widgets/content.dart
+++ b/lib/widgets/content.dart
@@ -820,7 +820,7 @@ class MathBlock extends StatelessWidget {
return Center(
child: SingleChildScrollViewWithScrollbar(
scrollDirection: Axis.horizontal,
- child: _Katex(
+ child: Katex(
textStyle: ContentTheme.of(context).textStylePlainParagraph,
nodes: nodes)));
}
@@ -841,8 +841,9 @@ TextStyle mkBaseKatexTextStyle(TextStyle style) {
fontStyle: FontStyle.normal);
}
-class _Katex extends StatelessWidget {
- const _Katex({
+class Katex extends StatelessWidget {
+ const Katex({
+ super.key,
required this.textStyle,
required this.nodes,
});
@@ -876,6 +877,7 @@ class _KatexNodeList extends StatelessWidget {
baseline: TextBaseline.alphabetic,
child: switch (e) {
KatexSpanNode() => _KatexSpan(e),
+ KatexVlistNode() => _KatexVlist(e),
});
}))));
}
@@ -993,6 +995,23 @@ class _KatexSpan extends StatelessWidget {
}
}
+class _KatexVlist extends StatelessWidget {
+ const _KatexVlist(this.node);
+
+ final KatexVlistNode node;
+
+ @override
+ Widget build(BuildContext context) {
+ final em = DefaultTextStyle.of(context).style.fontSize!;
+
+ return Stack(children: List.unmodifiable(node.rows.map((row) {
+ return Transform.translate(
+ offset: Offset(0, row.verticalOffsetEm * em),
+ child: _KatexSpan(row.node));
+ })));
+ }
+}
+
class WebsitePreview extends StatelessWidget {
const WebsitePreview({super.key, required this.node});
@@ -1311,7 +1330,7 @@ class _InlineContentBuilder {
: WidgetSpan(
alignment: PlaceholderAlignment.baseline,
baseline: TextBaseline.alphabetic,
- child: _Katex(textStyle: widget.style, nodes: nodes));
+ child: Katex(textStyle: widget.style, nodes: nodes));
case GlobalTimeNode():
return WidgetSpan(alignment: PlaceholderAlignment.middle,
diff --git a/test/model/content_test.dart b/test/model/content_test.dart
index 315baa5e4c..82e2265441 100644
--- a/test/model/content_test.dart
+++ b/test/model/content_test.dart
@@ -895,6 +895,406 @@ class ContentExample {
]),
]);
+ static const mathBlockKatexVertical1 = ContentExample(
+ 'math block katex vertical 1',
+ // https://chat.zulip.org/#narrow/channel/7-test-here/topic/Rajesh/near/2176734
+ '```math\na\'\n```',
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ 'a'
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ '′
',
+ [
+ MathBlockNode(
+ texSource: 'a\'',
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(heightEm: 0.8019),
+ text: null,
+ nodes: []),
+ KatexSpanNode(
+ styles: KatexSpanStyles(),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(
+ fontFamily: 'KaTeX_Math',
+ fontStyle: KatexSpanFontStyle.italic),
+ text: 'a',
+ nodes: null),
+ KatexSpanNode(
+ styles: KatexSpanStyles(textAlign: KatexSpanTextAlign.left),
+ text: null,
+ nodes: [
+ KatexVlistNode(
+ rows: [
+ KatexVlistRowNode(
+ verticalOffsetEm: -3.113 + 2.7,
+ node: KatexSpanNode(
+ styles: KatexSpanStyles(marginRightEm: 0.05),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(fontSizeEm: 0.7),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(),
+ text: '′',
+ nodes: null),
+ ]),
+ ]),
+ ])),
+ ]),
+ ]),
+ ]),
+ ]),
+ ]),
+ ]);
+
+ static const mathBlockKatexVertical2 = ContentExample(
+ 'math block katex vertical 2',
+ // https://chat.zulip.org/#narrow/channel/7-test-here/topic/Rajesh/near/2176735
+ '```math\nx_n\n```',
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ 'x'
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ 'n'
+ ''
+ ''
+ '
',
+ [
+ MathBlockNode(
+ texSource: 'x_n',
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(
+ heightEm: 0.5806,
+ verticalAlignEm: -0.15),
+ text: null,
+ nodes: []),
+ KatexSpanNode(
+ styles: KatexSpanStyles(),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(
+ fontFamily: 'KaTeX_Math',
+ fontStyle: KatexSpanFontStyle.italic),
+ text: 'x',
+ nodes: null),
+ KatexSpanNode(
+ styles: KatexSpanStyles(textAlign: KatexSpanTextAlign.left),
+ text: null,
+ nodes: [
+ KatexVlistNode(
+ rows: [
+ KatexVlistRowNode(
+ verticalOffsetEm: -2.55 + 2.7,
+ node: KatexSpanNode(
+ styles: KatexSpanStyles(
+ marginLeftEm: 0,
+ marginRightEm: 0.05),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(fontSizeEm: 0.7),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(
+ fontFamily: 'KaTeX_Math',
+ fontStyle: KatexSpanFontStyle.italic),
+ text: 'n',
+ nodes: null),
+ ]),
+ ])),
+ ]),
+ ]),
+ ]),
+ ]),
+ ]),
+ ]);
+
+ static const mathBlockKatexVertical3 = ContentExample(
+ 'math block katex vertical 3',
+ // https://chat.zulip.org/#narrow/channel/7-test-here/topic/Rajesh/near/2176737
+ '```math\ne^x\n```',
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ 'e'
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ 'x
',
+ [
+ MathBlockNode(
+ texSource: 'e^x',
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(heightEm: 0.7144),
+ text: null,
+ nodes: []),
+ KatexSpanNode(
+ styles: KatexSpanStyles(),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(
+ fontFamily: 'KaTeX_Math',
+ fontStyle: KatexSpanFontStyle.italic),
+ text: 'e',
+ nodes: null),
+ KatexSpanNode(
+ styles: KatexSpanStyles(textAlign: KatexSpanTextAlign.left),
+ text: null,
+ nodes: [
+ KatexVlistNode(
+ rows: [
+ KatexVlistRowNode(
+ verticalOffsetEm: -3.113 + 2.7,
+ node: KatexSpanNode(
+ styles: KatexSpanStyles(marginRightEm: 0.05),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(fontSizeEm: 0.7),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(
+ fontFamily: 'KaTeX_Math',
+ fontStyle: KatexSpanFontStyle.italic),
+ text: 'x',
+ nodes: null),
+ ]),
+ ])),
+ ]),
+ ]),
+ ]),
+ ]),
+ ]),
+ ]);
+
+ static const mathBlockKatexVertical4 = ContentExample(
+ 'math block katex vertical 4',
+ // https://chat.zulip.org/#narrow/channel/7-test-here/topic/Rajesh/near/2176738
+ '```math\n_u^o\n```',
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ 'u'
+ ''
+ ''
+ ''
+ 'o'
+ ''
+ ''
+ '
',
+ [
+ MathBlockNode(
+ texSource: "_u^o",
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(
+ heightEm: 0.9614,
+ verticalAlignEm: -0.247),
+ text: null,
+ nodes: []),
+ KatexSpanNode(
+ styles: KatexSpanStyles(),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(),
+ text: null,
+ nodes: []),
+ KatexSpanNode(
+ styles: KatexSpanStyles(textAlign: KatexSpanTextAlign.left),
+ text: null,
+ nodes: [
+ KatexVlistNode(
+ rows: [
+ KatexVlistRowNode(
+ verticalOffsetEm: -2.453 + 2.7,
+ node: KatexSpanNode(
+ styles: KatexSpanStyles(marginRightEm: 0.05),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(fontSizeEm: 0.7),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(
+ fontFamily: 'KaTeX_Math',
+ fontStyle: KatexSpanFontStyle.italic),
+ text: 'u',
+ nodes: null),
+ ]),
+ ])),
+ KatexVlistRowNode(
+ verticalOffsetEm: -3.113 + 2.7,
+ node: KatexSpanNode(
+ styles: KatexSpanStyles(marginRightEm: 0.05),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(fontSizeEm: 0.7),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(
+ fontFamily: 'KaTeX_Math',
+ fontStyle: KatexSpanFontStyle.italic),
+ text: 'o',
+ nodes: null),
+ ]),
+ ])),
+ ]),
+ ]),
+ ]),
+ ]),
+ ]),
+ ]);
+
+ static const mathBlockKatexVertical5 = ContentExample(
+ 'math block katex vertical 5',
+ // https://chat.zulip.org/#narrow/channel/7-test-here/topic/Rajesh/near/2176739
+ '```math\na\\raisebox{0.25em}{\$b\$}c\n```',
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ 'a'
+ ''
+ ''
+ ''
+ ''
+ ''
+ ''
+ 'b'
+ 'c
',
+ [
+ MathBlockNode(
+ texSource: 'a\\raisebox{0.25em}{\$b\$}c',
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(heightEm: 0.9444),
+ text: null,
+ nodes: []),
+ KatexSpanNode(
+ styles: KatexSpanStyles(
+ fontFamily: 'KaTeX_Math',
+ fontStyle: KatexSpanFontStyle.italic),
+ text: 'a',
+ nodes: null),
+ KatexVlistNode(
+ rows: [
+ KatexVlistRowNode(
+ verticalOffsetEm: -3.25 + 3,
+ node: KatexSpanNode(
+ styles: KatexSpanStyles(),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(),
+ text: null,
+ nodes: [
+ KatexSpanNode(
+ styles: KatexSpanStyles(
+ fontFamily: 'KaTeX_Math',
+ fontStyle: KatexSpanFontStyle.italic),
+ text: 'b',
+ nodes: null),
+ ]),
+ ])),
+ ]),
+ KatexSpanNode(
+ styles: KatexSpanStyles(
+ fontFamily: 'KaTeX_Math',
+ fontStyle: KatexSpanFontStyle.italic),
+ text: 'c',
+ nodes: null),
+ ]),
+ ]),
+ ]);
+
static const imageSingle = ContentExample(
'single image',
// https://chat.zulip.org/#narrow/stream/7-test-here/topic/Thumbnails/near/1900103
@@ -1963,6 +2363,11 @@ void main() async {
testParseExample(ContentExample.mathBlockKatexSizing);
testParseExample(ContentExample.mathBlockKatexNestedSizing);
testParseExample(ContentExample.mathBlockKatexDelimSizing);
+ testParseExample(ContentExample.mathBlockKatexVertical1);
+ testParseExample(ContentExample.mathBlockKatexVertical2);
+ testParseExample(ContentExample.mathBlockKatexVertical3);
+ testParseExample(ContentExample.mathBlockKatexVertical4);
+ testParseExample(ContentExample.mathBlockKatexVertical5);
testParseExample(ContentExample.imageSingle);
testParseExample(ContentExample.imageSingleNoDimensions);
diff --git a/test/widgets/content_test.dart b/test/widgets/content_test.dart
index d5445fb931..e5a00dc73d 100644
--- a/test/widgets/content_test.dart
+++ b/test/widgets/content_test.dart
@@ -1,10 +1,14 @@
import 'package:checks/checks.dart';
+import 'package:file/file.dart';
+import 'package:file/local.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
+import 'package:flutter/services.dart';
import 'package:flutter_checks/flutter_checks.dart';
import 'package:flutter_test/flutter_test.dart';
+import 'package:path/path.dart' as p;
import 'package:url_launcher/url_launcher.dart';
import 'package:zulip/api/core.dart';
import 'package:zulip/model/content.dart';
@@ -672,6 +676,59 @@ void main() {
fontHeight: baseTextStyle.height!);
}
});
+
+ group('characters render at specific offsets with specific size: ', () {
+ final testCases = [
+ (ContentExample.mathBlockKatexVertical1, [
+ ('a', Offset(0.0, 5.28), Size(10.88, 25.0)),
+ ('′', Offset(10.88, 1.12), Size(3.95, 17.0)),
+ ]),
+ (ContentExample.mathBlockKatexVertical2, [
+ ('x', Offset(0.0, 5.28), Size(11.76, 25.0)),
+ ('n', Offset(11.76, 13.65), Size(8.63, 17.0)),
+ ]),
+ (ContentExample.mathBlockKatexVertical3, [
+ ('e', Offset(0.0, 5.28), Size(9.58, 25.0)),
+ ('x', Offset(9.58, 2.07), Size(8.23, 17.0)),
+ ]),
+ (ContentExample.mathBlockKatexVertical4, [
+ ('u', Offset(0.0, 15.64), Size(8.23, 17.0)),
+ ('o', Offset(0.0, 2.07), Size(6.98, 17.0)),
+ ]),
+ (ContentExample.mathBlockKatexVertical5, [
+ ('a', Offset(0.0, 4.16), Size(10.88, 25.0)),
+ ('b', Offset(10.88, -0.65), Size(8.82, 25.0)),
+ ('c', Offset(19.70, 4.16), Size(8.90, 25.0)),
+ ]),
+ ];
+
+ for (final testCase in testCases) {
+ testWidgets(testCase.$1.description, (tester) async {
+ await _loadKatexFonts();
+
+ addTearDown(testBinding.reset);
+ final globalSettings = testBinding.globalStore.settings;
+ await globalSettings.setBool(BoolGlobalSetting.renderKatex, true);
+ check(globalSettings).getBool(BoolGlobalSetting.renderKatex).isTrue();
+
+ await prepareContent(tester, plainContent(testCase.$1.html));
+
+ final baseRect = tester.getRect(find.byType(Katex));
+
+ for (final characterData in testCase.$2) {
+ final character = characterData.$1;
+ final topLeftOffset = characterData.$2;
+ final size = characterData.$3;
+
+ final rect = tester.getRect(find.text(character));
+ check(rect.topLeft - baseRect.topLeft)
+ .within(distance: 0.01, from: topLeftOffset);
+ check(rect.size)
+ .within(distance: 0.01, from: size);
+ }
+ });
+ }
+ });
});
/// Make a [TargetFontSizeFinder] to pass to [checkFontSizeRatio],
@@ -1418,3 +1475,21 @@ void main() {
});
});
}
+
+Future _loadKatexFonts() async {
+ const fs = LocalFileSystem();
+ final katexFontsDir = fs.currentDirectory.childDirectory(
+ fs.path.join('assets', 'KaTeX'));
+ assert(katexFontsDir.existsSync());
+
+ for (final fontFile in katexFontsDir.listSync()) {
+ if (fontFile is! File) continue;
+ if (p.extension(fontFile.path) != '.ttf') continue;
+
+ final [fontName, ...] = p.withoutExtension(fontFile.basename).split('-');
+
+ final bytes =
+ Future.value(fontFile.readAsBytesSync().buffer.asByteData());
+ await (FontLoader(fontName)..addFont(bytes)).load();
+ }
+}
\ No newline at end of file