Skip to content

Commit

Permalink
feat: Add ability to convert between TextPaint and InlineTextStyle (#…
Browse files Browse the repository at this point in the history
…3128)

Add ability to convert between `TextPaint` and `InlineTextStyle`.

The opposite is already possible with `asTextRenderer`, which this
exposes.
  • Loading branch information
luanpotter committed Apr 18, 2024
1 parent 8332a68 commit 6b63a57
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/flame/lib/src/text/renderers/text_paint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,15 @@ class TextPaint extends TextRenderer {
textDirection: textDirection ?? this.textDirection,
);
}

InlineTextStyle asInlineTextStyle() {
return InlineTextStyle(
color: style.color,
fontFamily: style.fontFamily,
fontSize: style.fontSize,
fontWeight: style.fontWeight,
fontStyle: style.fontStyle,
letterSpacing: style.letterSpacing,
);
}
}
1 change: 0 additions & 1 deletion packages/flame/lib/src/text/styles/inline_text_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class InlineTextStyle extends FlameTextStyle {
);
}

@internal
TextPaint asTextRenderer() {
return TextPaint(
style: TextStyle(
Expand Down
31 changes: 31 additions & 0 deletions packages/flame/test/text/text_paint_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:ui' show Color;

import 'package:flame/text.dart';
import 'package:flutter/rendering.dart' as flutter;
import 'package:test/test.dart';
Expand All @@ -11,5 +13,34 @@ void main() {
expect(tp.style.fontSize, 12);
expect(tp.style.fontFamily, 'Helvetica');
});

test(
'can convert back and forth between TextPaint and InlineTextStyle',
() {
const flutterStyle = flutter.TextStyle(
fontSize: 12,
fontFamily: 'Times',
fontStyle: flutter.FontStyle.italic,
fontWeight: flutter.FontWeight.bold,
color: Color(0xFF00FF00),
letterSpacing: 1.5,
);
final textPaint = TextPaint(style: flutterStyle);

final inlineTextStyle = textPaint.asInlineTextStyle();
expect(inlineTextStyle.fontSize, 12);
expect(inlineTextStyle.fontFamily, 'Times');
expect(inlineTextStyle.fontStyle, flutter.FontStyle.italic);
expect(inlineTextStyle.fontWeight, flutter.FontWeight.bold);
expect(inlineTextStyle.color, const Color(0xFF00FF00));

final newTextPaint = inlineTextStyle.asTextRenderer();
expect(newTextPaint.style.fontSize, 12);
expect(newTextPaint.style.fontFamily, 'Times');
expect(newTextPaint.style.fontStyle, flutter.FontStyle.italic);
expect(newTextPaint.style.fontWeight, flutter.FontWeight.bold);
expect(newTextPaint.style.color, const Color(0xFF00FF00));
},
);
});
}

0 comments on commit 6b63a57

Please sign in to comment.