forked from firka/flutter
Use Skia to determine "actual" font name in CanvasKit backend (flutter/engine#16504)
* Use Skia to determine "actual" font name. This way, we don't need to hard-code a mapping of Flutter names to actual names, we can compute it when the font is registered. * Refactor to only have loaded and unloaded fonts
This commit is contained in:
@@ -14,25 +14,82 @@ const String _robotoUrl =
|
||||
|
||||
/// Manages the fonts used in the Skia-based backend.
|
||||
class SkiaFontCollection {
|
||||
final List<Future<ByteBuffer>> _loadingFontBuffers = <Future<ByteBuffer>>[];
|
||||
final List<Uint8List> _dynamicallyLoadedFonts = <Uint8List>[];
|
||||
/// Fonts that have been registered but haven't been loaded yet.
|
||||
final List<Future<_RegisteredFont>> _unloadedFonts =
|
||||
<Future<_RegisteredFont>>[];
|
||||
|
||||
/// 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<String, String> fontFamilyOverrides = <String, String>{};
|
||||
|
||||
final Set<String> registeredFamilies = <String>{};
|
||||
|
||||
Future<void> ensureFontsLoaded() async {
|
||||
await _loadFonts();
|
||||
_computeFontFamilyOverrides();
|
||||
|
||||
final List<Uint8List> fontBuffers =
|
||||
(await Future.wait<ByteBuffer>(_loadingFontBuffers))
|
||||
.map((ByteBuffer buffer) => buffer.asUint8List())
|
||||
.toList();
|
||||
fontBuffers.addAll(_dynamicallyLoadedFonts);
|
||||
_registeredFonts.map<Uint8List>((f) => f.bytes).toList();
|
||||
|
||||
skFontMgr = canvasKit['SkFontMgr'].callMethod('FromData', fontBuffers);
|
||||
}
|
||||
|
||||
Future<void> 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<void> _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<void> 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<String, dynamic> 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', <Uint8List>[bytes]);
|
||||
String actualFamily = tmpFontMgr.callMethod('getFamilyName', <int>[0]);
|
||||
return actualFamily;
|
||||
}
|
||||
|
||||
Future<ByteBuffer> _getArrayBuffer(dynamic fetchResult) {
|
||||
return fetchResult.arrayBuffer().then<ByteBuffer>((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);
|
||||
}
|
||||
|
||||
@@ -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<String, String> _fontFamilyOverrides = <String, String>{
|
||||
'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<String> fontFamilies = <String>[fontFamily];
|
||||
if (fontFamilyFallback != null) {
|
||||
if (fontFamilyFallback != null &&
|
||||
!fontFamilyFallback.every((font) => fontFamily == font)) {
|
||||
fontFamilies.addAll(fontFamilyFallback);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user