forked from firka/flutter
Add support for FontLoader API for the web (flutter/engine#13999)
This commit is contained in:
@@ -73,6 +73,10 @@ class FontCollection {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> 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<void> _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<void> ensureFontsLoaded() {
|
||||
|
||||
@@ -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<void> loadFontFromList(Uint8List list, {String fontFamily}) {
|
||||
if (engine.assertionsEnabled) {
|
||||
throw UnsupportedError('loadFontFromList is not supported.');
|
||||
}
|
||||
return Future<void>.value(null);
|
||||
return _fontCollection.loadFontFromList(list, fontFamily: fontFamily);
|
||||
}
|
||||
|
||||
@@ -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<void> 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<Exception>()));
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user