From e78dbf3347b3fb41e902fc8f9d69a13107638eb3 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Mon, 25 Nov 2019 11:07:13 -0800 Subject: [PATCH] Add support for FontLoader API for the web (flutter/engine#13999) --- .../lib/src/engine/text/font_collection.dart | 20 ++++++++ .../flutter/lib/web_ui/lib/src/ui/text.dart | 5 +- .../web_ui/test/text/font_loading_test.dart | 49 +++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 engine/src/flutter/lib/web_ui/test/text/font_loading_test.dart diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text/font_collection.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text/font_collection.dart index e33b9c3385..258c1fc253 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text/font_collection.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text/font_collection.dart @@ -73,6 +73,10 @@ class FontCollection { } } + Future loadFontFromList(Uint8List list, {String fontFamily}) { + return _assetFontManager._loadFontFaceBytes(fontFamily, list); + } + /// Registers fonts that are used by tests. void debugRegisterTestFonts() { _testFontManager = FontManager(); @@ -189,6 +193,22 @@ class FontManager { } } + // Loads a font from bytes, surfacing errors through the future. + Future _loadFontFaceBytes(String family, Uint8List list) { + // Since these fonts are loaded by user code, surface the error + // through the returned future. + final html.FontFace fontFace = html.FontFace(family, list); + return fontFace.load().then((_) { + html.document.fonts.add(fontFace); + }, onError: (dynamic exception) { + // Failures here will throw an html.DomException which confusingly + // does not implement Exception or Error. Rethrow an Exception so it can + // be caught in user code without depending on dart:html or requiring a + // catch block without "on". + throw Exception(exception.toString()); + }); + } + /// Returns a [Future] that completes when all fonts that have been /// registered with this font manager have been loaded and are ready to use. Future ensureFontsLoaded() { diff --git a/engine/src/flutter/lib/web_ui/lib/src/ui/text.dart b/engine/src/flutter/lib/web_ui/lib/src/ui/text.dart index c3dc8edf31..b85ef0d14e 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/ui/text.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/ui/text.dart @@ -1437,8 +1437,5 @@ abstract class ParagraphBuilder { /// * `fontFamily`: The family name used to identify the font in text styles. /// If this is not provided, then the family name will be extracted from the font file. Future loadFontFromList(Uint8List list, {String fontFamily}) { - if (engine.assertionsEnabled) { - throw UnsupportedError('loadFontFromList is not supported.'); - } - return Future.value(null); + return _fontCollection.loadFontFromList(list, fontFamily: fontFamily); } diff --git a/engine/src/flutter/lib/web_ui/test/text/font_loading_test.dart b/engine/src/flutter/lib/web_ui/test/text/font_loading_test.dart new file mode 100644 index 0000000000..a2d5945c0c --- /dev/null +++ b/engine/src/flutter/lib/web_ui/test/text/font_loading_test.dart @@ -0,0 +1,49 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:html' as html; +import 'dart:typed_data'; + +import 'package:test/test.dart'; +import 'package:ui/ui.dart' as ui; + +Future main() async { + await ui.webOnlyInitializeTestDomRenderer(); + group('loadFontFromList', () { + const String _testFontUrl = 'packages/ui/assets/ahem.ttf'; + + tearDown(() { + html.document.fonts.clear(); + }); + + test('surfaces error from invalid font buffer', () async { + await expectLater( + ui.loadFontFromList(Uint8List(0), fontFamily: 'test-font'), + throwsA(TypeMatcher())); + }); + + test('loads Blehm font from buffer', () async { + expect(_containsFontFamily('Blehm'), false); + + final html.HttpRequest response = await html.HttpRequest.request( + _testFontUrl, + responseType: 'arraybuffer'); + await ui.loadFontFromList(Uint8List.view(response.response), + fontFamily: 'Blehm'); + + expect(_containsFontFamily('Blehm'), true); + }); + }); +} + +bool _containsFontFamily(String family) { + bool found = false; + html.document.fonts.forEach((html.FontFace fontFace, + html.FontFace fontFaceAgain, html.FontFaceSet fontFaceSet) { + if (fontFace.family == family) { + found = true; + } + }); + return found; +}