Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

loadfontfromlist should send fontchange message to framework #14805

Merged
merged 7 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/ui/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be static, to prevent creating multiple instances

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not in a class. Do you mean put this field to be a static member for some class? I am not familiar with how dart compile the code, I thought define it in the scope of this file is the same thing as a static member of a class?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, reading fail on my part. it is fine as a top level

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';
15 changes: 14 additions & 1 deletion lib/web_ui/lib/src/ui/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved it out to a final variable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

'flutter/system',
_fontChangeMessage,
(_) {},
);
}
23 changes: 22 additions & 1 deletion lib/web_ui/test/text/font_loading_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
Expand All @@ -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"}');
});
});
}

Expand Down
19 changes: 19 additions & 0 deletions testing/dart/text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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"}');
});
});
}