Skip to content

Commit

Permalink
Add Ahem as a test font in CanvasKit mode (flutter#27652)
Browse files Browse the repository at this point in the history
* Add Ahem as a test font in CanvasKit mode

* Don't enable `debugEmulateFlutterDriver` by default since tests use
platform channels.

* Update goldens lock
  • Loading branch information
Harry Terkelsen authored Jul 23, 2021
1 parent 3929740 commit 653d9d9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/web_ui/dev/goldens_lock.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
repository: https://github.com/flutter/goldens.git
revision: 364782e0e6afb9e977524395c4376916340bf176
revision: f1baad46498ce1a754a15d02249827bb042b446b
14 changes: 12 additions & 2 deletions lib/web_ui/lib/src/engine/canvaskit/fonts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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, () => <SkFont>[])
Expand Down Expand Up @@ -139,6 +143,11 @@ class SkiaFontCollection {
}
}

Future<void> debugRegisterTestFonts() async {
_unloadedFonts.add(_registerFont(_ahemUrl, 'Ahem'));
FontFallbackData.instance.globalFontFallbacks.add('Ahem');
}

Future<RegisteredFont?> _registerFont(String url, String family) async {
ByteBuffer buffer;
try {
Expand All @@ -162,7 +171,8 @@ class SkiaFontCollection {
}

String? _readActualFamilyName(Uint8List bytes) {
final SkFontMgr tmpFontMgr = canvasKit.FontMgr.FromData(<Uint8List>[bytes])!;
final SkFontMgr tmpFontMgr =
canvasKit.FontMgr.FromData(<Uint8List>[bytes])!;
final String? actualFamily = tmpFontMgr.getFamilyName(0);
tmpFontMgr.delete();
return actualFamily;
Expand Down
20 changes: 14 additions & 6 deletions lib/web_ui/lib/src/ui/initialization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ part of ui;
Future<void> webOnlyInitializePlatform({
engine.AssetManager? assetManager,
}) {
final Future<void> initializationFuture = _initializePlatform(assetManager: assetManager);
final Future<void> 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
Expand Down Expand Up @@ -46,7 +47,8 @@ engine.FontCollection? _fontCollection;
bool _webOnlyIsInitialized = false;
bool get webOnlyIsInitialized => _webOnlyIsInitialized;
Future<void> 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;
}
Expand All @@ -68,18 +70,24 @@ Future<void> 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;
}
}

Expand Down

0 comments on commit 653d9d9

Please sign in to comment.