From 815195114e7f64dfdd89fb810f935f64ca542a5f Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Fri, 13 May 2022 15:04:06 -0700 Subject: [PATCH] allow flutter tester to disable font loading from asset bundle (flutter/engine#33323) --- .../ci/licenses_golden/licenses_flutter | 1 + engine/src/flutter/common/settings.h | 2 + engine/src/flutter/lib/ui/BUILD.gn | 2 + .../flutter/lib/ui/fixtures/FontManifest.json | 1 + engine/src/flutter/shell/common/engine.cc | 4 +- engine/src/flutter/shell/common/switches.cc | 2 + engine/src/flutter/shell/common/switches.h | 5 ++ .../src/flutter/testing/dart/assets_test.dart | 60 +++++++++++++++++-- engine/src/flutter/testing/run_tests.py | 3 +- 9 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 engine/src/flutter/lib/ui/fixtures/FontManifest.json diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 4232df3049..e405341d08 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -810,6 +810,7 @@ FILE: ../../../flutter/lib/ui/dart_ui.cc FILE: ../../../flutter/lib/ui/dart_ui.h FILE: ../../../flutter/lib/ui/dart_wrapper.h FILE: ../../../flutter/lib/ui/fixtures/DashInNooglerHat.jpg +FILE: ../../../flutter/lib/ui/fixtures/FontManifest.json FILE: ../../../flutter/lib/ui/fixtures/Horizontal.jpg FILE: ../../../flutter/lib/ui/fixtures/Horizontal.png FILE: ../../../flutter/lib/ui/fixtures/hello_loop_2.gif diff --git a/engine/src/flutter/common/settings.h b/engine/src/flutter/common/settings.h index 99dda16cbf..afae2b6440 100644 --- a/engine/src/flutter/common/settings.h +++ b/engine/src/flutter/common/settings.h @@ -190,6 +190,8 @@ struct Settings { // Font settings bool use_test_fonts = false; + bool use_asset_fonts = true; + // Indicates whether the embedding started a prefetch of the default font // manager before creating the engine. bool prefetched_default_font_manager = false; diff --git a/engine/src/flutter/lib/ui/BUILD.gn b/engine/src/flutter/lib/ui/BUILD.gn index 9f0b7526b0..3c1e7713ee 100644 --- a/engine/src/flutter/lib/ui/BUILD.gn +++ b/engine/src/flutter/lib/ui/BUILD.gn @@ -187,6 +187,8 @@ if (enable_unittests) { "fixtures/Horizontal.png", "fixtures/hello_loop_2.gif", "fixtures/hello_loop_2.webp", + "fixtures/FontManifest.json", + "//flutter/third_party/txt/third_party/fonts/Roboto-Medium.ttf", ] } diff --git a/engine/src/flutter/lib/ui/fixtures/FontManifest.json b/engine/src/flutter/lib/ui/fixtures/FontManifest.json new file mode 100644 index 0000000000..b0f7af848a --- /dev/null +++ b/engine/src/flutter/lib/ui/fixtures/FontManifest.json @@ -0,0 +1 @@ +[{"family":"Roboto","fonts":[{"asset":"Roboto-Medium.ttf"}]}] \ No newline at end of file diff --git a/engine/src/flutter/shell/common/engine.cc b/engine/src/flutter/shell/common/engine.cc index 7b3589ebfb..aa741632ae 100644 --- a/engine/src/flutter/shell/common/engine.cc +++ b/engine/src/flutter/shell/common/engine.cc @@ -172,7 +172,9 @@ bool Engine::UpdateAssetManager( } // Using libTXT as the text engine. - font_collection_->RegisterFonts(asset_manager_); + if (settings_.use_asset_fonts) { + font_collection_->RegisterFonts(asset_manager_); + } if (settings_.use_test_fonts) { font_collection_->RegisterTestFonts(); diff --git a/engine/src/flutter/shell/common/switches.cc b/engine/src/flutter/shell/common/switches.cc index 135fe0a9c6..871eb8377e 100644 --- a/engine/src/flutter/shell/common/switches.cc +++ b/engine/src/flutter/shell/common/switches.cc @@ -421,6 +421,8 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) { settings.use_test_fonts = command_line.HasOption(FlagForSwitch(Switch::UseTestFonts)); + settings.use_asset_fonts = + !command_line.HasOption(FlagForSwitch(Switch::DisableAssetFonts)); std::string enable_skparagraph = command_line.GetOptionValueWithDefault( FlagForSwitch(Switch::EnableSkParagraph), ""); diff --git a/engine/src/flutter/shell/common/switches.h b/engine/src/flutter/shell/common/switches.h index be393d88bc..a6512908ce 100644 --- a/engine/src/flutter/shell/common/switches.h +++ b/engine/src/flutter/shell/common/switches.h @@ -176,6 +176,11 @@ DEF_SWITCH(UseTestFonts, "will make font resolution default to the Ahem test font on all " "platforms (See https://www.w3.org/Style/CSS/Test/Fonts/Ahem/). " "This option is only available on the desktop test shells.") +DEF_SWITCH(DisableAssetFonts, + "disable-asset-fonts", + "Prevents usage of any non-test fonts unless they were explicitly " + "Loaded via dart:ui font APIs. This option is only available on the " + "desktop test shells.") DEF_SWITCH(PrefetchedDefaultFontManager, "prefetched-default-font-manager", "Indicates whether the embedding started a prefetch of the " diff --git a/engine/src/flutter/testing/dart/assets_test.dart b/engine/src/flutter/testing/dart/assets_test.dart index de975f4716..f05f6ad7a8 100644 --- a/engine/src/flutter/testing/dart/assets_test.dart +++ b/engine/src/flutter/testing/dart/assets_test.dart @@ -2,7 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:ui' as ui; +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; +import 'dart:typed_data'; +import 'dart:ui'; import 'package:litetest/litetest.dart'; @@ -10,7 +14,7 @@ void main() { test('Loading an asset that does not exist returns null', () async { Object? error; try { - await ui.ImmutableBuffer.fromAsset('ThisDoesNotExist'); + await ImmutableBuffer.fromAsset('ThisDoesNotExist'); } catch (err) { error = err; } @@ -19,14 +23,62 @@ void main() { }); test('returns the bytes of a bundled asset', () async { - final ui.ImmutableBuffer buffer = await ui.ImmutableBuffer.fromAsset('assets/DashInNooglerHat.jpg'); + final ImmutableBuffer buffer = await ImmutableBuffer.fromAsset('DashInNooglerHat.jpg'); expect(buffer.length == 354679, true); }); test('can dispose immutable buffer', () async { - final ui.ImmutableBuffer buffer = await ui.ImmutableBuffer.fromAsset('assets/DashInNooglerHat.jpg'); + final ImmutableBuffer buffer = await ImmutableBuffer.fromAsset('DashInNooglerHat.jpg'); buffer.dispose(); }); + + test('Tester can disable loading fonts from an asset bundle', () async { + final List ahemImage = await _createPictureFromFont('Ahem'); + // Font that is bundled in the asset directory of the test runner. + final List bundledFontImage = await _createPictureFromFont('Roboto'); + // Bundling fonts is disabled, so the font selected in both cases should be ahem. + // Therefore each buffer will contain identical contents. + expect(ahemImage, equals(bundledFontImage)); + }); + + test('Tester can still load through dart:ui', () async { + /// Manually load font asset through dart. + final Uint8List encoded = utf8.encoder.convert(Uri(path: Uri.encodeFull('Roboto-Medium.ttf')).path); + final Completer result = Completer(); + window.sendPlatformMessage('flutter/assets', encoded.buffer.asByteData(), (ByteData? data) { + result.complete(data!.buffer.asUint8List()); + }); + + await loadFontFromList(await result.future, fontFamily: 'Roboto2'); + + final List ahemImage = await _createPictureFromFont('Ahem'); + // Font that is bundled in the asset directory of the test runner. + final List bundledFontImage = await _createPictureFromFont('Roboto2'); + // Bundling fonts is disabled, so the font selected in both cases should be ahem. + // Therefore each buffer will contain identical contents. + expect(ahemImage, notEquals(bundledFontImage)); + }); +} + +Future> _createPictureFromFont(String fontFamily) async { + final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle( + fontFamily: fontFamily, + fontStyle: FontStyle.normal, + fontWeight: FontWeight.normal, + fontSize: 20, + )); + builder.addText('Test'); + final Paragraph paragraph = builder.build(); + paragraph.layout(const ParagraphConstraints(width: 20 * 5.0)); + + final PictureRecorder recorder = PictureRecorder(); + final Canvas canvas = Canvas(recorder); + canvas.drawParagraph(paragraph, Offset.zero); + + final Picture picture = recorder.endRecording(); + final Image image = await picture.toImage(100, 100); + final ByteData? data = await image.toByteData(); + return data!.buffer.asUint8List().toList(); } diff --git a/engine/src/flutter/testing/run_tests.py b/engine/src/flutter/testing/run_tests.py index 5100c2ee35..4251f7cb82 100755 --- a/engine/src/flutter/testing/run_tests.py +++ b/engine/src/flutter/testing/run_tests.py @@ -286,7 +286,8 @@ def RunDartTest(build_dir, test_packages, dart_file, verbose_dart_snapshot, mult command_args += [ '--use-test-fonts', '--icu-data-file-path=%s' % os.path.join(build_dir, 'icudtl.dat'), - '--flutter-assets-dir=%s' % os.path.join(build_dir, 'gen', 'flutter', 'lib', 'ui'), + '--flutter-assets-dir=%s' % os.path.join(build_dir, 'gen', 'flutter', 'lib', 'ui', 'assets'), + '--disable-asset-fonts', kernel_file_output, ]