Skip to content

Commit

Permalink
reland "Make FlutterTest the default test font" (flutter#40188)
Browse files Browse the repository at this point in the history
  • Loading branch information
LongCatIsLooong committed Mar 13, 2023
1 parent 57f7120 commit 2b8d324
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
9 changes: 6 additions & 3 deletions lib/web_ui/lib/src/engine/canvaskit/fonts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,15 @@ class SkiaFontCollection implements FontCollection {
}
}
final List<UnregisteredFont?> completedPendingFonts = await Future.wait(pendingFonts);
completedPendingFonts.add(UnregisteredFont(
final List<UnregisteredFont> fonts = <UnregisteredFont>[
UnregisteredFont(
EmbeddedTestFont.flutterTest.data.buffer,
'<embedded>',
EmbeddedTestFont.flutterTest.fontFamily,
));
_unregisteredFonts.addAll(completedPendingFonts.whereType<UnregisteredFont>());
),
...completedPendingFonts.whereType<UnregisteredFont>(),
];
_unregisteredFonts.addAll(fonts);

// Ahem must be added to font fallbacks list regardless of where it was
// downloaded from.
Expand Down
6 changes: 3 additions & 3 deletions lib/web_ui/lib/src/engine/text/font_collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ class HtmlFontCollection implements FontCollection {
@override
Future<void> debugDownloadTestFonts() async {
final FontManager fontManager = _testFontManager = FontManager();
for (final MapEntry<String, String> fontEntry in testFontUrls.entries) {
fontManager.downloadAsset(fontEntry.key, 'url(${fontEntry.value})', const <String, String>{});
}
fontManager._downloadedFonts.add(createDomFontFace(
EmbeddedTestFont.flutterTest.fontFamily,
EmbeddedTestFont.flutterTest.data,
));
for (final MapEntry<String, String> fontEntry in testFontUrls.entries) {
fontManager.downloadAsset(fontEntry.key, 'url(${fontEntry.value})', const <String, String>{});
}
await fontManager.downloadAllFonts();
}

Expand Down
9 changes: 9 additions & 0 deletions lib/web_ui/test/canvaskit/skia_font_collection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,14 @@ void testMain() {
expect(fontCollection.debugRegisteredFonts, isNotEmpty);
expect(warnings, isEmpty);
});

test('FlutterTest is the default test font', () async {
final SkiaFontCollection fontCollection = SkiaFontCollection();

await fontCollection.debugDownloadTestFonts();
fontCollection.registerDownloadedFonts();
expect(fontCollection.debugRegisteredFonts, isNotEmpty);
expect(fontCollection.debugRegisteredFonts!.first.family, 'FlutterTest');
});
});
}
6 changes: 3 additions & 3 deletions runtime/test_font_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1621,19 +1621,19 @@ namespace flutter {
std::vector<sk_sp<SkTypeface>> GetTestFontData() {
std::vector<sk_sp<SkTypeface>> typefaces;
#if EMBED_TEST_FONT_DATA
typefaces.push_back(SkTypeface::MakeFromStream(
SkMemoryStream::MakeDirect(kFlutterTestFont, kFlutterTestFontLength)));
typefaces.push_back(SkTypeface::MakeFromStream(
SkMemoryStream::MakeDirect(kAhemFont, kAhemFontLength)));
typefaces.push_back(SkTypeface::MakeFromStream(
SkMemoryStream::MakeDirect(kCoughFont, kCoughFontLength)));
typefaces.push_back(SkTypeface::MakeFromStream(
SkMemoryStream::MakeDirect(kFlutterTestFont, kFlutterTestFontLength)));
#endif // EMBED_TEST_FONT_DATA
return typefaces;
}

std::vector<std::string> GetTestFontFamilyNames() {
#if EMBED_TEST_FONT_DATA
std::vector<std::string> names = {"Ahem", "Cough", "FlutterTest"};
std::vector<std::string> names = {"FlutterTest", "Ahem", "Cough"};
#else // EMBED_TEST_FONT_DATA
std::vector<std::string> names;
#endif // EMBED_TEST_FONT_DATA
Expand Down
1 change: 1 addition & 0 deletions testing/dart/canvas_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ void main() {
builder.pushStyle(TextStyle(
decoration: TextDecoration.underline,
decorationColor: const Color(0xFF0000FF),
fontFamily: 'Ahem',
fontSize: 10,
color: const Color(0xFF000000),
decorationStyle: style,
Expand Down
19 changes: 8 additions & 11 deletions testing/dart/paragraph_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import 'dart:ui';
import 'package:litetest/litetest.dart';

void main() {
// The actual values for font measurements will vary by platform slightly.
const double epsillon = 0.0001;

test('Should be able to build and layout a paragraph', () {
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle());
builder.addText('Hello');
Expand Down Expand Up @@ -40,10 +37,10 @@ void main() {

final List<TextBox> boxes = paragraph.getBoxesForRange(0, 3);
expect(boxes.length, 1);
expect(boxes.first.left, 0);
expect(boxes.first.top, closeTo(0, epsillon));
expect(boxes.first.right, closeTo(42, epsillon));
expect(boxes.first.bottom, closeTo(14, epsillon));
expect(boxes.first.left, 0.0);
expect(boxes.first.top, 0.0);
expect(boxes.first.right, 42.0);
expect(boxes.first.bottom, 14.0);
expect(boxes.first.direction, TextDirection.ltr);
});

Expand All @@ -60,13 +57,13 @@ void main() {
final List<LineMetrics> metrics = paragraph.computeLineMetrics();
expect(metrics.length, 1);
expect(metrics.first.hardBreak, true);
expect(metrics.first.ascent, closeTo(11.200042724609375, epsillon));
expect(metrics.first.descent, closeTo(2.799957275390625, epsillon));
expect(metrics.first.unscaledAscent, closeTo(11.200042724609375, epsillon));
expect(metrics.first.ascent, 10.5);
expect(metrics.first.descent, 3.5);
expect(metrics.first.unscaledAscent, 10.5);
expect(metrics.first.height, 14.0);
expect(metrics.first.width, 70.0);
expect(metrics.first.left, 0.0);
expect(metrics.first.baseline, closeTo(11.200042724609375, epsillon));
expect(metrics.first.baseline, 10.5);
expect(metrics.first.lineNumber, 0);
});
}

0 comments on commit 2b8d324

Please sign in to comment.