Skip to content

Commit 4bf050b

Browse files
dawidopeTecHaxter
authored andcommitted
[flutter_markdown] Custom fontfeature superscript (flutter#5874)
Fix not solved issue from another PR: flutter#5058 (comment). I chose the 2nd option with custom font feature.
1 parent f438f65 commit 4bf050b

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

packages/flutter_markdown/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.7.1
2+
3+
* Allows for choosing a custom font feature to create superscript in footnotes when the font does not support the `supr` font feature.
4+
* Use the `superscriptFontFeatureTag` property in `MarkdownStyleSheet`.
5+
* For example, for the `Roboto` font which doesn't support `supr`, you can set `numr`.
6+
17
## 0.7.0
28

39
* **BREAKING CHANGES**:

packages/flutter_markdown/lib/src/builder.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,8 @@ class MarkdownBuilder implements md.NodeVisitor {
537537
style: textSpan.style?.copyWith(
538538
fontFeatures: <FontFeature>[
539539
const FontFeature.enable('sups'),
540+
if (styleSheet.superscriptFontFeatureTag != null)
541+
FontFeature.enable(styleSheet.superscriptFontFeatureTag!),
540542
],
541543
),
542544
),

packages/flutter_markdown/lib/src/style_sheet.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class MarkdownStyleSheet {
5959
this.orderedListAlign = WrapAlignment.start,
6060
this.blockquoteAlign = WrapAlignment.start,
6161
this.codeblockAlign = WrapAlignment.start,
62+
this.superscriptFontFeatureTag,
6263
@Deprecated('Use textScaler instead.') this.textScaleFactor,
6364
TextScaler? textScaler,
6465
}) : assert(
@@ -391,6 +392,7 @@ class MarkdownStyleSheet {
391392
WrapAlignment? orderedListAlign,
392393
WrapAlignment? blockquoteAlign,
393394
WrapAlignment? codeblockAlign,
395+
String? superscriptFontFeatureTag,
394396
@Deprecated('Use textScaler instead.') double? textScaleFactor,
395397
TextScaler? textScaler,
396398
}) {
@@ -457,6 +459,8 @@ class MarkdownStyleSheet {
457459
orderedListAlign: orderedListAlign ?? this.orderedListAlign,
458460
blockquoteAlign: blockquoteAlign ?? this.blockquoteAlign,
459461
codeblockAlign: codeblockAlign ?? this.codeblockAlign,
462+
superscriptFontFeatureTag:
463+
superscriptFontFeatureTag ?? this.superscriptFontFeatureTag,
460464
textScaler: newTextScaler,
461465
textScaleFactor: nextTextScaleFactor,
462466
);
@@ -520,6 +524,7 @@ class MarkdownStyleSheet {
520524
blockquoteAlign: other.blockquoteAlign,
521525
codeblockAlign: other.codeblockAlign,
522526
textScaleFactor: other.textScaleFactor,
527+
superscriptFontFeatureTag: other.superscriptFontFeatureTag,
523528
// Only one of textScaler and textScaleFactor can be passed. If
524529
// other.textScaleFactor is non-null, then the sheet was created with a
525530
// textScaleFactor and the textScaler was derived from that, so should be
@@ -688,6 +693,10 @@ class MarkdownStyleSheet {
688693
@Deprecated('Use textScaler instead.')
689694
final double? textScaleFactor;
690695

696+
/// Custom font feature tag for font which does not support `sups'
697+
/// feature to create superscript in footnotes.
698+
final String? superscriptFontFeatureTag;
699+
691700
/// A [Map] from element name to the corresponding [TextStyle] object.
692701
Map<String, TextStyle?> get styles => _styles;
693702
Map<String, TextStyle?> _styles;
@@ -752,6 +761,7 @@ class MarkdownStyleSheet {
752761
other.orderedListAlign == orderedListAlign &&
753762
other.blockquoteAlign == blockquoteAlign &&
754763
other.codeblockAlign == codeblockAlign &&
764+
other.superscriptFontFeatureTag == superscriptFontFeatureTag &&
755765
other.textScaler == textScaler;
756766
}
757767

@@ -811,6 +821,7 @@ class MarkdownStyleSheet {
811821
codeblockAlign,
812822
textScaler,
813823
textScaleFactor,
824+
superscriptFontFeatureTag,
814825
]);
815826
}
816827
}

packages/flutter_markdown/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: A Markdown renderer for Flutter. Create rich text output,
44
formatted with simple Markdown tags.
55
repository: https://github.com/flutter/packages/tree/main/packages/flutter_markdown
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_markdown%22
7-
version: 0.7.0
7+
version: 0.7.1
88

99
environment:
1010
sdk: ^3.3.0

packages/flutter_markdown/test/footnote_test.dart

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ void defineTests() {
158158
'superscript textstyle replacing',
159159
() {
160160
testWidgets(
161-
'superscript has correct fontfeature',
161+
'superscript has correct default fontfeature',
162162
(WidgetTester tester) async {
163163
const String data = 'Foo[^a]\n[^a]: Bar';
164164
await tester.pumpWidget(
@@ -184,6 +184,35 @@ void defineTests() {
184184
},
185185
);
186186

187+
testWidgets(
188+
'superscript has correct custom fontfeature',
189+
(WidgetTester tester) async {
190+
const String data = 'Foo[^a]\n[^a]: Bar';
191+
await tester.pumpWidget(
192+
boilerplate(
193+
MarkdownBody(
194+
data: data,
195+
styleSheet:
196+
MarkdownStyleSheet(superscriptFontFeatureTag: 'numr'),
197+
),
198+
),
199+
);
200+
201+
final Iterable<Widget> widgets = tester.allWidgets;
202+
final Text text =
203+
widgets.firstWhere((Widget widget) => widget is Text) as Text;
204+
205+
final TextSpan span = text.textSpan! as TextSpan;
206+
final List<InlineSpan>? children = span.children;
207+
208+
expect(children, isNotNull);
209+
expect(children!.length, 2);
210+
expect(children[1].style, isNotNull);
211+
expect(children[1].style!.fontFeatures?.length, 2);
212+
expect(children[1].style!.fontFeatures?[1].feature, 'numr');
213+
},
214+
);
215+
187216
testWidgets(
188217
'superscript index has the same font style like text',
189218
(WidgetTester tester) async {

0 commit comments

Comments
 (0)