Skip to content

Commit 0ec1641

Browse files
rajveermalviyagnprice
authored andcommitted
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 e8e8f41 commit 0ec1641

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

lib/widgets/content.dart

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -828,30 +828,39 @@ class MathBlock extends StatelessWidget {
828828
child: SingleChildScrollViewWithScrollbar(
829829
scrollDirection: Axis.horizontal,
830830
child: KatexWidget(
831+
textStyle: ContentTheme.of(context).textStylePlainParagraph,
831832
nodes: nodes))));
832833
}
833834
}
834835

835-
// Base text style from .katex class in katex.scss :
836-
// https://github.com/KaTeX/KaTeX/blob/613c3da8/src/styles/katex.scss#L13-L15
837-
const kBaseKatexTextStyle = TextStyle(
838-
fontSize: kBaseFontSize * 1.21,
839-
fontFamily: 'KaTeX_Main',
840-
height: 1.2,
841-
fontWeight: FontWeight.normal,
842-
fontStyle: FontStyle.normal,
843-
textBaseline: TextBaseline.alphabetic,
844-
leadingDistribution: TextLeadingDistribution.even,
845-
decoration: TextDecoration.none,
846-
fontFamilyFallback: []);
836+
/// Creates a base text style for rendering KaTeX content.
837+
///
838+
/// This applies the CSS styles defined in .katex class in katex.scss :
839+
/// https://github.com/KaTeX/KaTeX/blob/613c3da8/src/styles/katex.scss#L13-L15
840+
///
841+
/// Requires the [style.fontSize] to be non-null.
842+
TextStyle mkBaseKatexTextStyle(TextStyle style) {
843+
return style.copyWith(
844+
fontSize: style.fontSize! * 1.21,
845+
fontFamily: 'KaTeX_Main',
846+
height: 1.2,
847+
fontWeight: FontWeight.normal,
848+
fontStyle: FontStyle.normal,
849+
textBaseline: TextBaseline.alphabetic,
850+
leadingDistribution: TextLeadingDistribution.even,
851+
decoration: TextDecoration.none,
852+
fontFamilyFallback: const []);
853+
}
847854

848855
@visibleForTesting
849856
class KatexWidget extends StatelessWidget {
850857
const KatexWidget({
851858
super.key,
859+
required this.textStyle,
852860
required this.nodes,
853861
});
854862

863+
final TextStyle textStyle;
855864
final List<KatexNode> nodes;
856865

857866
@override
@@ -861,7 +870,7 @@ class KatexWidget extends StatelessWidget {
861870
return Directionality(
862871
textDirection: TextDirection.ltr,
863872
child: DefaultTextStyle(
864-
style: kBaseKatexTextStyle.copyWith(
873+
style: mkBaseKatexTextStyle(textStyle).copyWith(
865874
color: ContentTheme.of(context).textStylePlainParagraph.color),
866875
child: widget));
867876
}
@@ -1285,7 +1294,7 @@ class _InlineContentBuilder {
12851294
: WidgetSpan(
12861295
alignment: PlaceholderAlignment.baseline,
12871296
baseline: TextBaseline.alphabetic,
1288-
child: KatexWidget(nodes: nodes));
1297+
child: KatexWidget(textStyle: widget.style, nodes: nodes));
12891298

12901299
case GlobalTimeNode():
12911300
return WidgetSpan(alignment: PlaceholderAlignment.middle,

test/widgets/content_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,32 @@ void main() {
10421042
testContentSmoke(ContentExample.mathInline);
10431043

10441044
testWidgets('maintains font-size ratio with surrounding text', (tester) async {
1045+
addTearDown(testBinding.reset);
1046+
final globalSettings = testBinding.globalStore.settings;
1047+
await globalSettings.setBool(BoolGlobalSetting.renderKatex, true);
1048+
check(globalSettings.getBool(BoolGlobalSetting.renderKatex)).isTrue();
1049+
1050+
const html = '<span class="katex">'
1051+
'<span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>λ</mi></mrow>'
1052+
'<annotation encoding="application/x-tex"> \\lambda </annotation></semantics></math></span>'
1053+
'<span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">λ</span></span></span></span>';
1054+
await checkFontSizeRatio(tester,
1055+
targetHtml: html,
1056+
targetFontSizeFinder: (rootSpan) {
1057+
late final double result;
1058+
rootSpan.visitChildren((span) {
1059+
if (span case WidgetSpan(child: KatexWidget() && var widget)) {
1060+
result = mergedStyleOf(tester,
1061+
findAncestor: find.byWidget(widget), r'λ')!.fontSize!;
1062+
return false;
1063+
}
1064+
return true;
1065+
});
1066+
return result;
1067+
});
1068+
});
1069+
1070+
testWidgets('maintains font-size ratio with surrounding text, when showing TeX source', (tester) async {
10451071
const html = '<span class="katex">'
10461072
'<span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>λ</mi></mrow>'
10471073
'<annotation encoding="application/x-tex"> \\lambda </annotation></semantics></math></span>'

0 commit comments

Comments
 (0)