From 46ab6665b9448677342d61dabd67804dcbb93eee Mon Sep 17 00:00:00 2001 From: Dawid Wenderski Date: Thu, 11 Jan 2024 23:44:19 +0100 Subject: [PATCH 1/6] [flutter_markdown] allow to choose custom font feature for superscript --- packages/flutter_markdown/lib/src/builder.dart | 2 ++ packages/flutter_markdown/lib/src/style_sheet.dart | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/flutter_markdown/lib/src/builder.dart b/packages/flutter_markdown/lib/src/builder.dart index e89a6620c060..a633c36cc745 100644 --- a/packages/flutter_markdown/lib/src/builder.dart +++ b/packages/flutter_markdown/lib/src/builder.dart @@ -530,6 +530,8 @@ class MarkdownBuilder implements md.NodeVisitor { style: textSpan.style?.copyWith( fontFeatures: [ const FontFeature.enable('sups'), + if (styleSheet.superscriptFontFeatureTag != null) + FontFeature.enable(styleSheet.superscriptFontFeatureTag!), ], ), ), diff --git a/packages/flutter_markdown/lib/src/style_sheet.dart b/packages/flutter_markdown/lib/src/style_sheet.dart index 8d73d318aa9c..27e1c973279a 100644 --- a/packages/flutter_markdown/lib/src/style_sheet.dart +++ b/packages/flutter_markdown/lib/src/style_sheet.dart @@ -60,6 +60,7 @@ class MarkdownStyleSheet { this.blockquoteAlign = WrapAlignment.start, this.codeblockAlign = WrapAlignment.start, this.textScaleFactor, + this.superscriptFontFeatureTag, }) : _styles = { 'a': a, 'p': p, @@ -381,6 +382,7 @@ class MarkdownStyleSheet { WrapAlignment? blockquoteAlign, WrapAlignment? codeblockAlign, double? textScaleFactor, + String? superscriptFontFeatureTag, }) { return MarkdownStyleSheet( a: a ?? this.a, @@ -436,6 +438,8 @@ class MarkdownStyleSheet { blockquoteAlign: blockquoteAlign ?? this.blockquoteAlign, codeblockAlign: codeblockAlign ?? this.codeblockAlign, textScaleFactor: textScaleFactor ?? this.textScaleFactor, + superscriptFontFeatureTag: + superscriptFontFeatureTag ?? this.superscriptFontFeatureTag, ); } @@ -497,6 +501,7 @@ class MarkdownStyleSheet { blockquoteAlign: other.blockquoteAlign, codeblockAlign: other.codeblockAlign, textScaleFactor: other.textScaleFactor, + superscriptFontFeatureTag: other.superscriptFontFeatureTag, ); } @@ -653,6 +658,10 @@ class MarkdownStyleSheet { /// The text scale factor to use in textual elements final double? textScaleFactor; + // Custom font feature tag for font which does not support `sups' + // feature to create superscript in footnotes. + final String? superscriptFontFeatureTag; + /// A [Map] from element name to the corresponding [TextStyle] object. Map get styles => _styles; Map _styles; @@ -717,7 +726,8 @@ class MarkdownStyleSheet { other.orderedListAlign == orderedListAlign && other.blockquoteAlign == blockquoteAlign && other.codeblockAlign == codeblockAlign && - other.textScaleFactor == textScaleFactor; + other.textScaleFactor == textScaleFactor && + other.superscriptFontFeatureTag == superscriptFontFeatureTag; } @override @@ -775,6 +785,7 @@ class MarkdownStyleSheet { blockquoteAlign, codeblockAlign, textScaleFactor, + superscriptFontFeatureTag, ]); } } From e723138e3cc3e82ee932ee42687514b8764e1869 Mon Sep 17 00:00:00 2001 From: Dawid Wenderski Date: Thu, 11 Jan 2024 23:44:44 +0100 Subject: [PATCH 2/6] [flutter_markdown] test for custom font feature --- .../flutter_markdown/test/footnote_test.dart | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/flutter_markdown/test/footnote_test.dart b/packages/flutter_markdown/test/footnote_test.dart index b02b3af05256..406e89f7bce8 100644 --- a/packages/flutter_markdown/test/footnote_test.dart +++ b/packages/flutter_markdown/test/footnote_test.dart @@ -158,7 +158,7 @@ void defineTests() { 'superscript textstyle replacing', () { testWidgets( - 'superscript has correct fontfeature', + 'superscript has correct default fontfeature', (WidgetTester tester) async { const String data = 'Foo[^a]\n[^a]: Bar'; await tester.pumpWidget( @@ -184,6 +184,35 @@ void defineTests() { }, ); + testWidgets( + 'superscript has correct custom fontfeature', + (WidgetTester tester) async { + const String data = 'Foo[^a]\n[^a]: Bar'; + await tester.pumpWidget( + boilerplate( + MarkdownBody( + data: data, + styleSheet: + MarkdownStyleSheet(superscriptFontFeatureTag: 'numr'), + ), + ), + ); + + final Iterable widgets = tester.allWidgets; + final RichText richText = widgets + .firstWhere((Widget widget) => widget is RichText) as RichText; + + final TextSpan span = richText.text as TextSpan; + final List? children = span.children; + + expect(children, isNotNull); + expect(children!.length, 2); + expect(children[1].style, isNotNull); + expect(children[1].style!.fontFeatures?.length, 2); + expect(children[1].style!.fontFeatures?[1].feature, 'numr'); + }, + ); + testWidgets( 'superscript index has the same font style like text', (WidgetTester tester) async { From 4dfa2f916915f89be2884b7ce3c1ccc9b021ef48 Mon Sep 17 00:00:00 2001 From: Dawid Wenderski Date: Thu, 11 Jan 2024 23:51:23 +0100 Subject: [PATCH 3/6] [flutter_markdown] add changelog and bump version --- packages/flutter_markdown/CHANGELOG.md | 4 ++++ packages/flutter_markdown/pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/flutter_markdown/CHANGELOG.md b/packages/flutter_markdown/CHANGELOG.md index 5cb6371421fd..07454a790ae0 100644 --- a/packages/flutter_markdown/CHANGELOG.md +++ b/packages/flutter_markdown/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.6.18+4 + +* Allow for choosing custom font feature to create superscript in footnotes when the font does not support `supr` font feature. Use `superscriptFontFeatureTag` property in `MarkdownStyleSheet`. For example for `Roboto` font you can set `numr`. + ## 0.6.18+3 * Updates minimum supported SDK version to Flutter 3.10/Dart 3.0. diff --git a/packages/flutter_markdown/pubspec.yaml b/packages/flutter_markdown/pubspec.yaml index eb25a03ebd01..fe0723147a13 100644 --- a/packages/flutter_markdown/pubspec.yaml +++ b/packages/flutter_markdown/pubspec.yaml @@ -4,7 +4,7 @@ description: A Markdown renderer for Flutter. Create rich text output, formatted with simple Markdown tags. repository: https://github.com/flutter/packages/tree/main/packages/flutter_markdown issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_markdown%22 -version: 0.6.18+3 +version: 0.6.18+4 environment: sdk: ">=3.0.0 <4.0.0" From b7c3a80b2e5d0527e94bb141f7eb13f43722a1c0 Mon Sep 17 00:00:00 2001 From: Dawid Wenderski Date: Thu, 11 Jan 2024 23:59:18 +0100 Subject: [PATCH 4/6] [flutter_markdown] fix typo in doc comment --- packages/flutter_markdown/lib/src/style_sheet.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/flutter_markdown/lib/src/style_sheet.dart b/packages/flutter_markdown/lib/src/style_sheet.dart index 27e1c973279a..b240ad429f6b 100644 --- a/packages/flutter_markdown/lib/src/style_sheet.dart +++ b/packages/flutter_markdown/lib/src/style_sheet.dart @@ -658,8 +658,8 @@ class MarkdownStyleSheet { /// The text scale factor to use in textual elements final double? textScaleFactor; - // Custom font feature tag for font which does not support `sups' - // feature to create superscript in footnotes. + /// Custom font feature tag for font which does not support `sups' + /// feature to create superscript in footnotes. final String? superscriptFontFeatureTag; /// A [Map] from element name to the corresponding [TextStyle] object. From 6de425e6b66a4b4fdd1ef3066621b726ba99512c Mon Sep 17 00:00:00 2001 From: Dawid Wenderski Date: Wed, 10 Apr 2024 10:30:40 +0200 Subject: [PATCH 5/6] [flutter_markdown] format syntax --- .../flutter_markdown/lib/src/style_sheet.dart | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/flutter_markdown/lib/src/style_sheet.dart b/packages/flutter_markdown/lib/src/style_sheet.dart index 4e20fdfb4ad3..f3b570aeb914 100644 --- a/packages/flutter_markdown/lib/src/style_sheet.dart +++ b/packages/flutter_markdown/lib/src/style_sheet.dart @@ -69,7 +69,9 @@ class MarkdownStyleSheet { textScaler = textScaler ?? // Internally, only textScaler is used, so convert the scale factor // to a linear scaler. - (textScaleFactor == null ? null : TextScaler.linear(textScaleFactor)), + (textScaleFactor == null + ? null + : TextScaler.linear(textScaleFactor)), _styles = { 'a': a, 'p': p, @@ -164,7 +166,9 @@ class MarkdownStyleSheet { assert(theme.textTheme.textStyle.fontSize != null); return MarkdownStyleSheet( a: theme.textTheme.textStyle.copyWith( - color: theme.brightness == Brightness.dark ? CupertinoColors.link.darkColor : CupertinoColors.link.color, + color: theme.brightness == Brightness.dark + ? CupertinoColors.link.darkColor + : CupertinoColors.link.color, ), p: theme.textTheme.textStyle, pPadding: EdgeInsets.zero, @@ -398,8 +402,10 @@ class MarkdownStyleSheet { ); // If either of textScaler or textScaleFactor is non-null, pass null for the // other instead of the previous value, since only one is allowed. - final TextScaler? newTextScaler = textScaler ?? (textScaleFactor == null ? this.textScaler : null); - final double? nextTextScaleFactor = textScaleFactor ?? (textScaler == null ? this.textScaleFactor : null); + final TextScaler? newTextScaler = + textScaler ?? (textScaleFactor == null ? this.textScaler : null); + final double? nextTextScaleFactor = + textScaleFactor ?? (textScaler == null ? this.textScaleFactor : null); return MarkdownStyleSheet( a: a ?? this.a, p: p ?? this.p, @@ -434,12 +440,14 @@ class MarkdownStyleSheet { tableColumnWidth: tableColumnWidth ?? this.tableColumnWidth, tableCellsPadding: tableCellsPadding ?? this.tableCellsPadding, tableCellsDecoration: tableCellsDecoration ?? this.tableCellsDecoration, - tableVerticalAlignment: tableVerticalAlignment ?? this.tableVerticalAlignment, + tableVerticalAlignment: + tableVerticalAlignment ?? this.tableVerticalAlignment, blockquotePadding: blockquotePadding ?? this.blockquotePadding, blockquoteDecoration: blockquoteDecoration ?? this.blockquoteDecoration, codeblockPadding: codeblockPadding ?? this.codeblockPadding, codeblockDecoration: codeblockDecoration ?? this.codeblockDecoration, - horizontalRuleDecoration: horizontalRuleDecoration ?? this.horizontalRuleDecoration, + horizontalRuleDecoration: + horizontalRuleDecoration ?? this.horizontalRuleDecoration, textAlign: textAlign ?? this.textAlign, h1Align: h1Align ?? this.h1Align, h2Align: h2Align ?? this.h2Align, @@ -451,7 +459,8 @@ class MarkdownStyleSheet { orderedListAlign: orderedListAlign ?? this.orderedListAlign, blockquoteAlign: blockquoteAlign ?? this.blockquoteAlign, codeblockAlign: codeblockAlign ?? this.codeblockAlign, - superscriptFontFeatureTag: superscriptFontFeatureTag ?? this.superscriptFontFeatureTag, + superscriptFontFeatureTag: + superscriptFontFeatureTag ?? this.superscriptFontFeatureTag, textScaler: newTextScaler, textScaleFactor: nextTextScaleFactor, ); From 5a9e025aa826243056669f8006d2458b26744fa7 Mon Sep 17 00:00:00 2001 From: Dawid Wenderski Date: Wed, 10 Apr 2024 10:47:14 +0200 Subject: [PATCH 6/6] [flutter_markdown] fix test --- packages/flutter_markdown/test/footnote_test.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/flutter_markdown/test/footnote_test.dart b/packages/flutter_markdown/test/footnote_test.dart index 0c2941798d05..3be96c46093d 100644 --- a/packages/flutter_markdown/test/footnote_test.dart +++ b/packages/flutter_markdown/test/footnote_test.dart @@ -199,10 +199,10 @@ void defineTests() { ); final Iterable widgets = tester.allWidgets; - final RichText richText = widgets - .firstWhere((Widget widget) => widget is RichText) as RichText; + final Text text = + widgets.firstWhere((Widget widget) => widget is Text) as Text; - final TextSpan span = richText.text as TextSpan; + final TextSpan span = text.textSpan! as TextSpan; final List? children = span.children; expect(children, isNotNull);