From 397d6c2435b87ef16ae99c1926bbf6dd30df4437 Mon Sep 17 00:00:00 2001 From: Harry Terkelsen Date: Fri, 23 Jul 2021 12:13:26 -0700 Subject: [PATCH] Add Ahem as a test font in CanvasKit mode (flutter/engine#27652) * Add Ahem as a test font in CanvasKit mode * Don't enable `debugEmulateFlutterDriver` by default since tests use platform channels. * Update goldens lock --- .../flutter/lib/web_ui/dev/goldens_lock.yaml | 2 +- .../lib/src/engine/canvaskit/fonts.dart | 14 +++++++++++-- .../lib/web_ui/lib/src/ui/initialization.dart | 20 +++++++++++++------ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/dev/goldens_lock.yaml b/engine/src/flutter/lib/web_ui/dev/goldens_lock.yaml index 1d3af5898ba..6fbe3e98745 100644 --- a/engine/src/flutter/lib/web_ui/dev/goldens_lock.yaml +++ b/engine/src/flutter/lib/web_ui/dev/goldens_lock.yaml @@ -1,2 +1,2 @@ repository: https://github.com/flutter/goldens.git -revision: 364782e0e6afb9e977524395c4376916340bf176 +revision: f1baad46498ce1a754a15d02249827bb042b446b diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/fonts.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/fonts.dart index 46bf4ce82ba..0499ab9d7b7 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/fonts.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/fonts.dart @@ -19,6 +19,9 @@ import 'font_fallbacks.dart'; const String _robotoUrl = 'https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Me5WZLCzYlKw.ttf'; +// URL for the Ahem font, only used in tests. +const String _ahemUrl = 'packages/ui/assets/ahem.ttf'; + /// Manages the fonts used in the Skia-based backend. class SkiaFontCollection { /// Fonts that have been registered but haven't been loaded yet. @@ -47,7 +50,8 @@ class SkiaFontCollection { .add(SkFont(font.typeface)); } - for (RegisteredFont font in FontFallbackData.instance.registeredFallbackFonts) { + for (RegisteredFont font + in FontFallbackData.instance.registeredFallbackFonts) { fontProvider!.registerFont(font.bytes, font.family); familyToFontMap .putIfAbsent(font.family, () => []) @@ -139,6 +143,11 @@ class SkiaFontCollection { } } + Future debugRegisterTestFonts() async { + _unloadedFonts.add(_registerFont(_ahemUrl, 'Ahem')); + FontFallbackData.instance.globalFontFallbacks.add('Ahem'); + } + Future _registerFont(String url, String family) async { ByteBuffer buffer; try { @@ -162,7 +171,8 @@ class SkiaFontCollection { } String? _readActualFamilyName(Uint8List bytes) { - final SkFontMgr tmpFontMgr = canvasKit.FontMgr.FromData([bytes])!; + final SkFontMgr tmpFontMgr = + canvasKit.FontMgr.FromData([bytes])!; final String? actualFamily = tmpFontMgr.getFamilyName(0); tmpFontMgr.delete(); return actualFamily; diff --git a/engine/src/flutter/lib/web_ui/lib/src/ui/initialization.dart b/engine/src/flutter/lib/web_ui/lib/src/ui/initialization.dart index 1ab32c6d8cb..a42974509ba 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/ui/initialization.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/ui/initialization.dart @@ -7,7 +7,8 @@ part of ui; Future webOnlyInitializePlatform({ engine.AssetManager? assetManager, }) { - final Future initializationFuture = _initializePlatform(assetManager: assetManager); + final Future initializationFuture = + _initializePlatform(assetManager: assetManager); scheduleMicrotask(() { // Access [engine.lineLookup] to force the lazy unpacking of line break data // now. Removing this line won't break anything. It's just an optimization @@ -46,7 +47,8 @@ engine.FontCollection? _fontCollection; bool _webOnlyIsInitialized = false; bool get webOnlyIsInitialized => _webOnlyIsInitialized; Future webOnlySetAssetManager(engine.AssetManager assetManager) async { - assert(assetManager != null, 'Cannot set assetManager to null'); // ignore: unnecessary_null_comparison + assert(assetManager != null, + 'Cannot set assetManager to null'); // ignore: unnecessary_null_comparison if (assetManager == _assetManager) { return; } @@ -68,18 +70,24 @@ Future webOnlySetAssetManager(engine.AssetManager assetManager) async { } } - if (debugEmulateFlutterTesterEnvironment && !engine.useCanvasKit) { - _fontCollection!.debugRegisterTestFonts(); + if (debugEmulateFlutterTesterEnvironment) { + if (engine.useCanvasKit) { + engine.skiaFontCollection.debugRegisterTestFonts(); + } else { + _fontCollection!.debugRegisterTestFonts(); + } } } -bool get debugEmulateFlutterTesterEnvironment => _debugEmulateFlutterTesterEnvironment; +bool get debugEmulateFlutterTesterEnvironment => + _debugEmulateFlutterTesterEnvironment; set debugEmulateFlutterTesterEnvironment(bool value) { _debugEmulateFlutterTesterEnvironment = value; if (_debugEmulateFlutterTesterEnvironment) { const Size logicalSize = Size(800.0, 600.0); - engine.window.webOnlyDebugPhysicalSizeOverride = logicalSize * window.devicePixelRatio; + engine.window.webOnlyDebugPhysicalSizeOverride = + logicalSize * window.devicePixelRatio; } }