diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/fonts.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/fonts.dart index 0f36077e1f..53f774b18c 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/fonts.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/fonts.dart @@ -14,25 +14,82 @@ const String _robotoUrl = /// Manages the fonts used in the Skia-based backend. class SkiaFontCollection { - final List> _loadingFontBuffers = >[]; - final List _dynamicallyLoadedFonts = []; + /// Fonts that have been registered but haven't been loaded yet. + final List> _unloadedFonts = + >[]; + + /// Fonts which have been registered and loaded. + final List<_RegisteredFont> _registeredFonts = <_RegisteredFont>[]; + + /// A mapping from the name a font was registered with, to the family name + /// embedded in the font's bytes (the font's "actual" name). + /// + /// For example, a font may be registered in Flutter assets with the name + /// "MaterialIcons", but if you read the family name out of the font's bytes + /// it is actually "Material Icons". Skia works with the actual names of the + /// fonts, so when we create a Skia Paragraph with Flutter font families, we + /// must convert them to their actual family name when we pass them to Skia. + final Map fontFamilyOverrides = {}; final Set registeredFamilies = {}; Future ensureFontsLoaded() async { + await _loadFonts(); + _computeFontFamilyOverrides(); + final List fontBuffers = - (await Future.wait(_loadingFontBuffers)) - .map((ByteBuffer buffer) => buffer.asUint8List()) - .toList(); - fontBuffers.addAll(_dynamicallyLoadedFonts); + _registeredFonts.map((f) => f.bytes).toList(); + skFontMgr = canvasKit['SkFontMgr'].callMethod('FromData', fontBuffers); } - Future loadFontFromList(Uint8List list, {String fontFamily}) async { - _dynamicallyLoadedFonts.add(list); - if (fontFamily != null) { - registeredFamilies.add(fontFamily); + /// Loads all of the unloaded fonts in [_unloadedFonts] and adds them + /// to [_registeredFonts]. + Future _loadFonts() async { + if (_unloadedFonts.isEmpty) return; + + final List<_RegisteredFont> loadedFonts = await Future.wait(_unloadedFonts); + _registeredFonts.addAll(loadedFonts.where((x) => x != null)); + _unloadedFonts.clear(); + } + + void _computeFontFamilyOverrides() { + fontFamilyOverrides.clear(); + + for (_RegisteredFont font in _registeredFonts) { + if (fontFamilyOverrides.containsKey(font.flutterFamily)) { + if (fontFamilyOverrides[font.flutterFamily] != font.actualFamily) { + html.window.console.warn('Fonts in family ${font.flutterFamily} ' + 'have different actual family names.'); + html.window.console.warn( + 'Current actual family: ${fontFamilyOverrides[font.flutterFamily]}'); + html.window.console.warn('New actual family: ${font.actualFamily}'); + } + } else { + fontFamilyOverrides[font.flutterFamily] = font.actualFamily; + } } + } + + Future loadFontFromList(Uint8List list, {String fontFamily}) async { + String actualFamily = _readActualFamilyName(list); + + if (actualFamily == null) { + if (fontFamily == null) { + html.window.console + .warn('Failed to read font family name. Aborting font load.'); + return; + } + actualFamily = fontFamily; + } + + if (fontFamily == null) { + fontFamily = actualFamily; + } + + registeredFamilies.add(fontFamily); + + _registeredFonts.add(_RegisteredFont(list, fontFamily, actualFamily)); await ensureFontsLoaded(); } @@ -72,9 +129,8 @@ class SkiaFontCollection { for (dynamic fontAssetItem in fontAssets) { final Map fontAsset = fontAssetItem; final String asset = fontAsset['asset']; - _loadingFontBuffers.add(html.window - .fetch(assetManager.getAssetUrl(asset)) - .then(_getArrayBuffer)); + _unloadedFonts + .add(_registerFont(assetManager.getAssetUrl(asset), family)); } } @@ -83,15 +139,59 @@ class SkiaFontCollection { /// Roboto to match Android. if (!registeredFamilies.contains('Roboto')) { // Download Roboto and add it to the font buffers. - _loadingFontBuffers.add(html.window - .fetch(_robotoUrl) - .then(_getArrayBuffer)); + _unloadedFonts.add(_registerFont(_robotoUrl, 'Roboto')); } } + Future<_RegisteredFont> _registerFont(String url, String family) async { + ByteBuffer buffer; + try { + buffer = await html.window.fetch(url).then(_getArrayBuffer); + } catch (e) { + html.window.console.warn('Failed to load font $family at $url'); + html.window.console.warn(e); + return null; + } + + final Uint8List bytes = buffer.asUint8List(); + String actualFamily = _readActualFamilyName(bytes); + + if (actualFamily == null) { + html.window.console.warn('Failed to determine the actual name of the ' + 'font $family at $url. Defaulting to $family.'); + actualFamily = family; + } + + return _RegisteredFont(bytes, family, actualFamily); + } + + String _readActualFamilyName(Uint8List bytes) { + final js.JsObject tmpFontMgr = + canvasKit['SkFontMgr'].callMethod('FromData', [bytes]); + String actualFamily = tmpFontMgr.callMethod('getFamilyName', [0]); + return actualFamily; + } + Future _getArrayBuffer(dynamic fetchResult) { return fetchResult.arrayBuffer().then((x) => x as ByteBuffer); } js.JsObject skFontMgr; } + +/// Represents a font that has been registered. +class _RegisteredFont { + /// The font family that the font was declared to have by Flutter. + final String flutterFamily; + + /// The byte data for this font. + final Uint8List bytes; + + /// The font family that was parsed from the font's bytes. + final String actualFamily; + + _RegisteredFont(this.bytes, this.flutterFamily, this.actualFamily) + : assert(bytes != null), + assert(flutterFamily != null), + assert(actualFamily != null); +} diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/text.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/text.dart index 99713e0807..f09fbbc90d 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/text.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/text.dart @@ -3,20 +3,6 @@ // found in the LICENSE file. part of engine; -// TODO(hterkelsen): Get rid of this once we can register font families with -// custom names. -/// CanvasKit does not yet allow us to specify family names when registering -/// fonts. CanvasKit reads the font name from the font's bytes. So, we map -/// some common family names to how they are registered in the Gallery app. -const Map _fontFamilyOverrides = { - 'GoogleSans': 'Google Sans', - 'GoogleSansDisplay': 'Google Sans Display', - 'MaterialIcons': 'Material Icons', - 'LibreFranklin': 'Libre Franklin', - 'AbrilFatface': 'Abril Fatface', - 'packages/cupertino_icons/CupertinoIcons': 'CupertinoIcons', -}; - class SkParagraphStyle implements ui.ParagraphStyle { SkParagraphStyle({ ui.TextAlign textAlign, @@ -72,8 +58,8 @@ class SkParagraphStyle implements ui.ParagraphStyle { !skiaFontCollection.registeredFamilies.contains(fontFamily)) { fontFamily = 'Roboto'; } - if (_fontFamilyOverrides.containsKey(fontFamily)) { - fontFamily = _fontFamilyOverrides[fontFamily]; + if (skiaFontCollection.fontFamilyOverrides.containsKey(fontFamily)) { + fontFamily = skiaFontCollection.fontFamilyOverrides[fontFamily]; } skTextStyle['fontFamilies'] = [fontFamily]; @@ -213,11 +199,12 @@ class SkTextStyle implements ui.TextStyle { fontFamily = 'Roboto'; } - if (_fontFamilyOverrides.containsKey(fontFamily)) { - fontFamily = _fontFamilyOverrides[fontFamily]; + if (skiaFontCollection.fontFamilyOverrides.containsKey(fontFamily)) { + fontFamily = skiaFontCollection.fontFamilyOverrides[fontFamily]; } List fontFamilies = [fontFamily]; - if (fontFamilyFallback != null) { + if (fontFamilyFallback != null && + !fontFamilyFallback.every((font) => fontFamily == font)) { fontFamilies.addAll(fontFamilyFallback); }