Skip to content

Commit 3e80a0d

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 af7d9df commit 3e80a0d

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

lib/widgets/content.dart

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -818,24 +818,37 @@ class MathBlock extends StatelessWidget {
818818
children: [TextSpan(text: node.texSource)])));
819819
}
820820

821-
return _Katex(inline: false, nodes: nodes);
821+
return _Katex(
822+
inline: false,
823+
textStyle: ContentTheme.of(context).textStylePlainParagraph,
824+
nodes: nodes);
822825
}
823826
}
824827

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

832843
class _Katex extends StatelessWidget {
833844
const _Katex({
834845
required this.inline,
846+
required this.textStyle,
835847
required this.nodes,
836848
});
837849

838850
final bool inline;
851+
final TextStyle textStyle;
839852
final List<KatexNode> nodes;
840853

841854
@override
@@ -851,9 +864,8 @@ class _Katex extends StatelessWidget {
851864

852865
return Directionality(
853866
textDirection: TextDirection.ltr,
854-
child: DefaultTextStyle(
855-
style: kBaseKatexTextStyle.copyWith(
856-
color: ContentTheme.of(context).textStylePlainParagraph.color),
867+
child: DefaultTextStyle.merge(
868+
style: mkBaseKatexTextStyle(textStyle),
857869
child: widget));
858870
}
859871
}
@@ -870,7 +882,9 @@ class _KatexNodeList extends StatelessWidget {
870882
return WidgetSpan(
871883
alignment: PlaceholderAlignment.baseline,
872884
baseline: TextBaseline.alphabetic,
873-
child: _KatexSpan(e));
885+
child: MediaQuery(
886+
data: MediaQueryData(textScaler: TextScaler.noScaling),
887+
child: _KatexSpan(e)));
874888
}))));
875889
}
876890
}
@@ -1263,7 +1277,7 @@ class _InlineContentBuilder {
12631277
: WidgetSpan(
12641278
alignment: PlaceholderAlignment.baseline,
12651279
baseline: TextBaseline.alphabetic,
1266-
child: _Katex(inline: true, nodes: nodes));
1280+
child: _Katex(inline: true, textStyle: widget.style, nodes: nodes));
12671281

12681282
case GlobalTimeNode():
12691283
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,15 +595,19 @@ 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;
600604
final nodes = baseNode.nodes!.skip(1); // Skip .strut node.
601605
for (final katexNode in nodes) {
602-
final fontSize = katexNode.styles.fontSizeEm! * kBaseKatexTextStyle.fontSize!;
606+
final fontSize = katexNode.styles.fontSizeEm! * baseTextStyle.fontSize!;
603607
checkKatexText(tester, katexNode.text!,
604608
fontFamily: 'KaTeX_Main',
605609
fontSize: fontSize,
606-
fontHeight: kBaseKatexTextStyle.height!);
610+
fontHeight: baseTextStyle.height!);
607611
}
608612
});
609613

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

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

625633
fontSize = 4.976 * fontSize;
626634
checkKatexText(tester, '2',
627635
fontFamily: 'KaTeX_Main',
628636
fontSize: fontSize,
629-
fontHeight: kBaseKatexTextStyle.height!);
637+
fontHeight: baseTextStyle.height!);
630638
});
631639

632640
testWidgets('displays KaTeX content with different delimiter sizing', (tester) async {
@@ -642,22 +650,24 @@ void main() {
642650
final baseNode = mathBlockNode.nodes!.single;
643651
var nodes = baseNode.nodes!.skip(1); // Skip .strut node.
644652

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

647657
final firstNode = nodes.first;
648658
checkKatexText(tester, firstNode.text!,
649659
fontFamily: 'KaTeX_Main',
650-
fontSize: fontSize,
651-
fontHeight: kBaseKatexTextStyle.height!);
660+
fontSize: baseTextStyle.fontSize!,
661+
fontHeight: baseTextStyle.height!);
652662
nodes = nodes.skip(1);
653663

654664
for (var katexNode in nodes) {
655665
katexNode = katexNode.nodes!.single; // Skip empty .mord parent.
656666
final fontFamily = katexNode.styles.fontFamily!;
657667
checkKatexText(tester, katexNode.text!,
658668
fontFamily: fontFamily,
659-
fontSize: fontSize,
660-
fontHeight: kBaseKatexTextStyle.height!);
669+
fontSize: baseTextStyle.fontSize!,
670+
fontHeight: baseTextStyle.height!);
661671
}
662672
});
663673
});

0 commit comments

Comments
 (0)