loadfontfromlist should send fontchange message to framework (flutter/engine#14805)

This commit is contained in:
chunhtai
2020-01-16 17:03:04 -08:00
committed by Flutter GitHub Bot
parent 1a91d5fb85
commit 0955ec77b3
4 changed files with 69 additions and 3 deletions

View File

@@ -2123,7 +2123,20 @@ class ParagraphBuilder extends NativeFieldWrapperClass2 {
Future<void> loadFontFromList(Uint8List list, {String fontFamily}) {
return _futurize(
(_Callback<void> callback) => _loadFontFromList(list, callback, fontFamily)
);
).then((_) => _sendFontChangeMessage());
}
final ByteData _fontChangeMessage = utf8.encoder.convert(
json.encode(<String, dynamic>{'type': 'fontsChange'})
).buffer.asByteData();
FutureOr<void> _sendFontChangeMessage() async {
if (window.onPlatformMessage != null)
window.onPlatformMessage(
'flutter/system',
_fontChangeMessage,
(_) {},
);
}
String _loadFontFromList(Uint8List list, _Callback<void> callback, String fontFamily) native 'loadFontFromList';

View File

@@ -1498,5 +1498,18 @@ 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}) {
return _fontCollection.loadFontFromList(list, fontFamily: fontFamily);
return _fontCollection.loadFontFromList(list, fontFamily: fontFamily).then(
(_) => _sendFontChangeMessage()
);
}
final ByteData _fontChangeMessage = engine.JSONMessageCodec().encodeMessage(<String, dynamic>{'type': 'fontsChange'});
FutureOr<void> _sendFontChangeMessage() async {
if (window.onPlatformMessage != null)
window.onPlatformMessage(
'flutter/system',
_fontChangeMessage,
(_) {},
);
}

View File

@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert';
import 'dart:html' as html;
import 'dart:typed_data';
@@ -36,7 +37,7 @@ Future<void> main() async {
expect(_containsFontFamily('Blehm'), true);
});
test('loads font should clear measurement caches', () async {
test('loading font should clear measurement caches', () async {
final ui.ParagraphStyle style = ui.ParagraphStyle();
final ui.ParagraphBuilder builder = ui.ParagraphBuilder(style);
final ui.ParagraphConstraints constraints = ui.ParagraphConstraints(width: 30.0);
@@ -58,6 +59,26 @@ Future<void> main() async {
expect(_containsFontFamily('Blehm'), true);
expect(TextMeasurementService.rulerManager.rulers.length, 0);
});
test('loading font should send font change message', () async {
final ui.PlatformMessageCallback oldHandler = ui.window.onPlatformMessage;
String actualName;
String message;
window.onPlatformMessage = (String name, ByteData data, ui.PlatformMessageResponseCallback callback) {
actualName = name;
final buffer = data.buffer;
final Uint8List list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
message = utf8.decode(list);
};
final html.HttpRequest response = await html.HttpRequest.request(
_testFontUrl,
responseType: 'arraybuffer');
await ui.loadFontFromList(Uint8List.view(response.response),
fontFamily: 'Blehm');
window.onPlatformMessage = oldHandler;
expect(actualName, 'flutter/system');
expect(message, '{"type":"fontsChange"}');
});
});
}

View File

@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert';
import 'dart:typed_data';
import 'dart:ui';
import 'package:test/test.dart';
@@ -79,4 +81,21 @@ void main() {
expect(const TextRange(start: 0, end: 5).textInside('hello'), equals('hello'));
});
});
group('loadFontFromList', () {
test('will send platform message after font is loaded', () async {
final PlatformMessageCallback oldHandler = window.onPlatformMessage;
String actualName;
String message;
window.onPlatformMessage = (String name, ByteData data, PlatformMessageResponseCallback callback) {
actualName = name;
final Uint8List list = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
message = utf8.decode(list);
};
final Uint8List fontData = Uint8List(0);
await loadFontFromList(fontData, fontFamily: 'fake');
window.onPlatformMessage = oldHandler;
expect(actualName, 'flutter/system');
expect(message, '{"type":"fontsChange"}');
});
});
}