From 2b8d324605db281cc1f04273cc0764b20a7d4cfd Mon Sep 17 00:00:00 2001 From: LongCat is Looong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Sun, 12 Mar 2023 20:03:55 -0700 Subject: [PATCH] reland "Make FlutterTest the default test font" (#40188) --- .../lib/src/engine/canvaskit/fonts.dart | 9 ++++++--- .../lib/src/engine/text/font_collection.dart | 6 +++--- .../canvaskit/skia_font_collection_test.dart | 9 +++++++++ runtime/test_font_data.cc | 6 +++--- testing/dart/canvas_test.dart | 1 + testing/dart/paragraph_builder_test.dart | 19 ++++++++----------- 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/lib/web_ui/lib/src/engine/canvaskit/fonts.dart b/lib/web_ui/lib/src/engine/canvaskit/fonts.dart index 84475d4f82a28..234d7908b99e6 100644 --- a/lib/web_ui/lib/src/engine/canvaskit/fonts.dart +++ b/lib/web_ui/lib/src/engine/canvaskit/fonts.dart @@ -184,12 +184,15 @@ class SkiaFontCollection implements FontCollection { } } final List completedPendingFonts = await Future.wait(pendingFonts); - completedPendingFonts.add(UnregisteredFont( + final List fonts = [ + UnregisteredFont( EmbeddedTestFont.flutterTest.data.buffer, '', EmbeddedTestFont.flutterTest.fontFamily, - )); - _unregisteredFonts.addAll(completedPendingFonts.whereType()); + ), + ...completedPendingFonts.whereType(), + ]; + _unregisteredFonts.addAll(fonts); // Ahem must be added to font fallbacks list regardless of where it was // downloaded from. diff --git a/lib/web_ui/lib/src/engine/text/font_collection.dart b/lib/web_ui/lib/src/engine/text/font_collection.dart index 7c863d5e87c2e..3865deb3343ea 100644 --- a/lib/web_ui/lib/src/engine/text/font_collection.dart +++ b/lib/web_ui/lib/src/engine/text/font_collection.dart @@ -76,13 +76,13 @@ class HtmlFontCollection implements FontCollection { @override Future debugDownloadTestFonts() async { final FontManager fontManager = _testFontManager = FontManager(); - for (final MapEntry fontEntry in testFontUrls.entries) { - fontManager.downloadAsset(fontEntry.key, 'url(${fontEntry.value})', const {}); - } fontManager._downloadedFonts.add(createDomFontFace( EmbeddedTestFont.flutterTest.fontFamily, EmbeddedTestFont.flutterTest.data, )); + for (final MapEntry fontEntry in testFontUrls.entries) { + fontManager.downloadAsset(fontEntry.key, 'url(${fontEntry.value})', const {}); + } await fontManager.downloadAllFonts(); } diff --git a/lib/web_ui/test/canvaskit/skia_font_collection_test.dart b/lib/web_ui/test/canvaskit/skia_font_collection_test.dart index 2650e586fa864..75764a8c35f4f 100644 --- a/lib/web_ui/test/canvaskit/skia_font_collection_test.dart +++ b/lib/web_ui/test/canvaskit/skia_font_collection_test.dart @@ -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'); + }); }); } diff --git a/runtime/test_font_data.cc b/runtime/test_font_data.cc index 9f86698dab1f1..9389079a8bbd1 100644 --- a/runtime/test_font_data.cc +++ b/runtime/test_font_data.cc @@ -1621,19 +1621,19 @@ namespace flutter { std::vector> GetTestFontData() { std::vector> 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 GetTestFontFamilyNames() { #if EMBED_TEST_FONT_DATA - std::vector names = {"Ahem", "Cough", "FlutterTest"}; + std::vector names = {"FlutterTest", "Ahem", "Cough"}; #else // EMBED_TEST_FONT_DATA std::vector names; #endif // EMBED_TEST_FONT_DATA diff --git a/testing/dart/canvas_test.dart b/testing/dart/canvas_test.dart index c230382017025..451beaf243af5 100644 --- a/testing/dart/canvas_test.dart +++ b/testing/dart/canvas_test.dart @@ -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, diff --git a/testing/dart/paragraph_builder_test.dart b/testing/dart/paragraph_builder_test.dart index 37e27a758147a..14761f17f9c83 100644 --- a/testing/dart/paragraph_builder_test.dart +++ b/testing/dart/paragraph_builder_test.dart @@ -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'); @@ -40,10 +37,10 @@ void main() { final List 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); }); @@ -60,13 +57,13 @@ void main() { final List 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); }); }