Skip to content

Commit

Permalink
[flutter_markdown] Custom fontfeature superscript (#5874)
Browse files Browse the repository at this point in the history
Fix not solved issue from another PR: #5058 (comment).

I chose the 2nd option with custom font feature.
  • Loading branch information
dawidope authored Apr 19, 2024
1 parent 136e73b commit f630fcd
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/flutter_markdown/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.7.1

* Allows for choosing a custom font feature to create superscript in footnotes when the font does not support the `supr` font feature.
* Use the `superscriptFontFeatureTag` property in `MarkdownStyleSheet`.
* For example, for the `Roboto` font which doesn't support `supr`, you can set `numr`.

## 0.7.0

* **BREAKING CHANGES**:
Expand Down
2 changes: 2 additions & 0 deletions packages/flutter_markdown/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,8 @@ class MarkdownBuilder implements md.NodeVisitor {
style: textSpan.style?.copyWith(
fontFeatures: <FontFeature>[
const FontFeature.enable('sups'),
if (styleSheet.superscriptFontFeatureTag != null)
FontFeature.enable(styleSheet.superscriptFontFeatureTag!),
],
),
),
Expand Down
11 changes: 11 additions & 0 deletions packages/flutter_markdown/lib/src/style_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class MarkdownStyleSheet {
this.orderedListAlign = WrapAlignment.start,
this.blockquoteAlign = WrapAlignment.start,
this.codeblockAlign = WrapAlignment.start,
this.superscriptFontFeatureTag,
@Deprecated('Use textScaler instead.') this.textScaleFactor,
TextScaler? textScaler,
}) : assert(
Expand Down Expand Up @@ -391,6 +392,7 @@ class MarkdownStyleSheet {
WrapAlignment? orderedListAlign,
WrapAlignment? blockquoteAlign,
WrapAlignment? codeblockAlign,
String? superscriptFontFeatureTag,
@Deprecated('Use textScaler instead.') double? textScaleFactor,
TextScaler? textScaler,
}) {
Expand Down Expand Up @@ -457,6 +459,8 @@ class MarkdownStyleSheet {
orderedListAlign: orderedListAlign ?? this.orderedListAlign,
blockquoteAlign: blockquoteAlign ?? this.blockquoteAlign,
codeblockAlign: codeblockAlign ?? this.codeblockAlign,
superscriptFontFeatureTag:
superscriptFontFeatureTag ?? this.superscriptFontFeatureTag,
textScaler: newTextScaler,
textScaleFactor: nextTextScaleFactor,
);
Expand Down Expand Up @@ -520,6 +524,7 @@ class MarkdownStyleSheet {
blockquoteAlign: other.blockquoteAlign,
codeblockAlign: other.codeblockAlign,
textScaleFactor: other.textScaleFactor,
superscriptFontFeatureTag: other.superscriptFontFeatureTag,
// Only one of textScaler and textScaleFactor can be passed. If
// other.textScaleFactor is non-null, then the sheet was created with a
// textScaleFactor and the textScaler was derived from that, so should be
Expand Down Expand Up @@ -688,6 +693,10 @@ class MarkdownStyleSheet {
@Deprecated('Use textScaler instead.')
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<String, TextStyle?> get styles => _styles;
Map<String, TextStyle?> _styles;
Expand Down Expand Up @@ -752,6 +761,7 @@ class MarkdownStyleSheet {
other.orderedListAlign == orderedListAlign &&
other.blockquoteAlign == blockquoteAlign &&
other.codeblockAlign == codeblockAlign &&
other.superscriptFontFeatureTag == superscriptFontFeatureTag &&
other.textScaler == textScaler;
}

Expand Down Expand Up @@ -811,6 +821,7 @@ class MarkdownStyleSheet {
codeblockAlign,
textScaler,
textScaleFactor,
superscriptFontFeatureTag,
]);
}
}
2 changes: 1 addition & 1 deletion packages/flutter_markdown/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.7.0
version: 0.7.1

environment:
sdk: ^3.3.0
Expand Down
31 changes: 30 additions & 1 deletion packages/flutter_markdown/test/footnote_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<Widget> widgets = tester.allWidgets;
final Text text =
widgets.firstWhere((Widget widget) => widget is Text) as Text;

final TextSpan span = text.textSpan! as TextSpan;
final List<InlineSpan>? 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 {
Expand Down

0 comments on commit f630fcd

Please sign in to comment.