Skip to content

Commit 0434863

Browse files
content: Scale inline KaTeX content based on the surrounding text
This applies the correct font scaling if the KaTeX content is inside a header.
1 parent 0bf40b8 commit 0434863

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

lib/widgets/content.dart

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -822,22 +822,33 @@ class MathBlock extends StatelessWidget {
822822
child: SingleChildScrollViewWithScrollbar(
823823
scrollDirection: Axis.horizontal,
824824
child: _Katex(
825+
textStyle: ContentTheme.of(context).textStylePlainParagraph,
825826
nodes: nodes)));
826827
}
827828
}
828829

829-
// Base text style from .katex class in katex.scss :
830-
// https://github.com/KaTeX/KaTeX/blob/613c3da8/src/styles/katex.scss#L13-L15
831-
const kBaseKatexTextStyle = TextStyle(
832-
fontSize: kBaseFontSize * 1.21,
833-
fontFamily: 'KaTeX_Main',
834-
height: 1.2);
830+
/// Creates a base text style for rendering KaTeX content.
831+
///
832+
/// This applies the CSS styles defined in .katex class in katex.scss :
833+
/// https://github.com/KaTeX/KaTeX/blob/613c3da8/src/styles/katex.scss#L13-L15
834+
///
835+
/// Requires the [style.fontSize] to be non-null.
836+
TextStyle mkBaseKatexTextStyle(TextStyle style) {
837+
return style.copyWith(
838+
fontSize: style.fontSize! * 1.21,
839+
fontFamily: 'KaTeX_Main',
840+
height: 1.2,
841+
fontWeight: FontWeight.normal,
842+
fontStyle: FontStyle.normal);
843+
}
835844

836845
class _Katex extends StatelessWidget {
837846
const _Katex({
847+
required this.textStyle,
838848
required this.nodes,
839849
});
840850

851+
final TextStyle textStyle;
841852
final List<KatexNode> nodes;
842853

843854
@override
@@ -846,9 +857,8 @@ class _Katex extends StatelessWidget {
846857

847858
return Directionality(
848859
textDirection: TextDirection.ltr,
849-
child: DefaultTextStyle(
850-
style: kBaseKatexTextStyle.copyWith(
851-
color: ContentTheme.of(context).textStylePlainParagraph.color),
860+
child: DefaultTextStyle.merge(
861+
style: mkBaseKatexTextStyle(textStyle),
852862
child: widget));
853863
}
854864
}
@@ -865,9 +875,11 @@ class _KatexNodeList extends StatelessWidget {
865875
return WidgetSpan(
866876
alignment: PlaceholderAlignment.baseline,
867877
baseline: TextBaseline.alphabetic,
868-
child: switch (e) {
869-
KatexSpanNode() => _KatexSpan(e),
870-
});
878+
child: MediaQuery(
879+
data: MediaQueryData(textScaler: TextScaler.noScaling),
880+
child: switch (e) {
881+
KatexSpanNode() => _KatexSpan(e),
882+
}));
871883
}))));
872884
}
873885
}
@@ -1266,7 +1278,7 @@ class _InlineContentBuilder {
12661278
: WidgetSpan(
12671279
alignment: PlaceholderAlignment.baseline,
12681280
baseline: TextBaseline.alphabetic,
1269-
child: _Katex(nodes: nodes));
1281+
child: _Katex(textStyle: widget.style, nodes: nodes));
12701282

12711283
case GlobalTimeNode():
12721284
return WidgetSpan(alignment: PlaceholderAlignment.middle,

test/widgets/content_test.dart

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -595,16 +595,20 @@ void main() {
595595
final content = ContentExample.mathBlockKatexSizing;
596596
await prepareContent(tester, plainContent(content.html));
597597

598+
final context = tester.element(find.byType(MathBlock));
599+
final baseTextStyle =
600+
mkBaseKatexTextStyle(ContentTheme.of(context).textStylePlainParagraph);
601+
598602
final mathBlockNode = content.expectedNodes.single as MathBlockNode;
599603
final baseNode = mathBlockNode.nodes!.single as KatexSpanNode;
600604
final nodes = baseNode.nodes!.skip(1); // Skip .strut node.
601605
for (var katexNode in nodes) {
602606
katexNode = katexNode as KatexSpanNode;
603-
final fontSize = katexNode.styles.fontSizeEm! * kBaseKatexTextStyle.fontSize!;
607+
final fontSize = katexNode.styles.fontSizeEm! * baseTextStyle.fontSize!;
604608
checkKatexText(tester, katexNode.text!,
605609
fontFamily: 'KaTeX_Main',
606610
fontSize: fontSize,
607-
fontHeight: kBaseKatexTextStyle.height!);
611+
fontHeight: baseTextStyle.height!);
608612
}
609613
});
610614

@@ -617,17 +621,21 @@ void main() {
617621
final content = ContentExample.mathBlockKatexNestedSizing;
618622
await prepareContent(tester, plainContent(content.html));
619623

620-
var fontSize = 0.5 * kBaseKatexTextStyle.fontSize!;
624+
final context = tester.element(find.byType(MathBlock));
625+
final baseTextStyle =
626+
mkBaseKatexTextStyle(ContentTheme.of(context).textStylePlainParagraph);
627+
628+
var fontSize = 0.5 * baseTextStyle.fontSize!;
621629
checkKatexText(tester, '1',
622630
fontFamily: 'KaTeX_Main',
623631
fontSize: fontSize,
624-
fontHeight: kBaseKatexTextStyle.height!);
632+
fontHeight: baseTextStyle.height!);
625633

626634
fontSize = 4.976 * fontSize;
627635
checkKatexText(tester, '2',
628636
fontFamily: 'KaTeX_Main',
629637
fontSize: fontSize,
630-
fontHeight: kBaseKatexTextStyle.height!);
638+
fontHeight: baseTextStyle.height!);
631639
});
632640

633641
testWidgets('displays KaTeX content with different delimiter sizing', (tester) async {
@@ -643,13 +651,15 @@ void main() {
643651
final baseNode = mathBlockNode.nodes!.single as KatexSpanNode;
644652
var nodes = baseNode.nodes!.skip(1); // Skip .strut node.
645653

646-
final fontSize = kBaseKatexTextStyle.fontSize!;
654+
final context = tester.element(find.byType(MathBlock));
655+
final baseTextStyle =
656+
mkBaseKatexTextStyle(ContentTheme.of(context).textStylePlainParagraph);
647657

648658
final firstNode = nodes.first as KatexSpanNode;
649659
checkKatexText(tester, firstNode.text!,
650660
fontFamily: 'KaTeX_Main',
651-
fontSize: fontSize,
652-
fontHeight: kBaseKatexTextStyle.height!);
661+
fontSize: baseTextStyle.fontSize!,
662+
fontHeight: baseTextStyle.height!);
653663
nodes = nodes.skip(1);
654664

655665
for (var katexNode in nodes) {
@@ -658,8 +668,8 @@ void main() {
658668
final fontFamily = katexNode.styles.fontFamily!;
659669
checkKatexText(tester, katexNode.text!,
660670
fontFamily: fontFamily,
661-
fontSize: fontSize,
662-
fontHeight: kBaseKatexTextStyle.height!);
671+
fontSize: baseTextStyle.fontSize!,
672+
fontHeight: baseTextStyle.height!);
663673
}
664674
}, skip: true); // TODO: Re-enable this test after adding support for parsing
665675
// `vertical-align` in inline styles. Currently it fails

0 commit comments

Comments
 (0)