Skip to content

Commit b433cc1

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 d7973b4 commit b433cc1

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

lib/widgets/content.dart

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -817,24 +817,34 @@ class MathBlock extends StatelessWidget {
817817
children: [TextSpan(text: node.texSource)])));
818818
}
819819

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

824827
// Base text style from .katex class in katex.scss :
825828
// https://github.com/KaTeX/KaTeX/blob/613c3da8/src/styles/katex.scss#L13-L15
826-
const kBaseKatexTextStyle = TextStyle(
827-
fontSize: kBaseFontSize * 1.21,
828-
fontFamily: 'KaTeX_Main',
829-
height: 1.2);
829+
TextStyle mkBaseKatexTextStyle(TextStyle style) {
830+
assert(style.fontSize != null);
831+
return style.copyWith(
832+
fontSize: style.fontSize! * 1.21,
833+
fontFamily: 'KaTeX_Main',
834+
height: 1.2,
835+
fontWeight: FontWeight.normal,
836+
fontStyle: FontStyle.normal);
837+
}
830838

831839
class _Katex extends StatelessWidget {
832840
const _Katex({
833841
required this.inline,
842+
required this.textStyle,
834843
required this.nodes,
835844
});
836845

837846
final bool inline;
847+
final TextStyle textStyle;
838848
final List<KatexNode> nodes;
839849

840850
@override
@@ -851,8 +861,7 @@ class _Katex extends StatelessWidget {
851861
return Directionality(
852862
textDirection: TextDirection.ltr,
853863
child: DefaultTextStyle(
854-
style: kBaseKatexTextStyle.copyWith(
855-
color: ContentTheme.of(context).textStylePlainParagraph.color),
864+
style: mkBaseKatexTextStyle(textStyle),
856865
child: widget));
857866
}
858867
}
@@ -1275,7 +1284,7 @@ class _InlineContentBuilder {
12751284
: WidgetSpan(
12761285
alignment: PlaceholderAlignment.baseline,
12771286
baseline: TextBaseline.alphabetic,
1278-
child: _Katex(inline: true, nodes: nodes));
1287+
child: _Katex(inline: true, textStyle: widget.style, nodes: nodes));
12791288

12801289
case GlobalTimeNode():
12811290
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)