Skip to content

Commit 766954e

Browse files
caijwmdebbar
authored andcommitted
perf: web ui loadFontFromList (flutter#181440)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> Optimize the loadFontFromList method of SkiaFontCollection. Reduce the generation of unnecessary FontMgr. issue: flutter#181439 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. Co-authored-by: Mouad Debbar <mdebbar@google.com>
1 parent bf03c0f commit 766954e

2 files changed

Lines changed: 13 additions & 16 deletions

File tree

engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2114,7 +2114,10 @@ extension type SkFontVariation._(JSObject _) implements JSObject {
21142114
external set value(double? v);
21152115
}
21162116

2117-
extension type SkTypeface(JSObject _) implements JSObject {}
2117+
extension type SkTypeface(JSObject _) implements JSObject {
2118+
/// Returns the family name of the typeface.
2119+
external String? getFamilyName();
2120+
}
21182121

21192122
@JS('window.flutterCanvasKit.Font')
21202123
extension type SkFont._(JSObject _) implements JSObject {

engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/fonts.dart

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,20 @@ class SkiaFontCollection implements FlutterFontCollection {
7272

7373
@override
7474
Future<bool> loadFontFromList(Uint8List list, {String? fontFamily}) async {
75-
if (fontFamily == null) {
76-
fontFamily = _readActualFamilyName(list);
77-
if (fontFamily == null) {
78-
printWarning('Failed to read font family name. Aborting font load.');
79-
return false;
80-
}
81-
}
82-
8375
// Make sure CanvasKit is actually loaded
8476
await renderer.initialize();
8577

8678
final SkTypeface? typeface = canvasKit.Typeface.MakeFreeTypeFaceFromData(list.buffer);
79+
8780
if (typeface != null) {
81+
if (fontFamily == null) {
82+
// Read actual family name from SkTypeface
83+
fontFamily = typeface.getFamilyName();
84+
if (fontFamily == null) {
85+
printWarning('Failed to read font family name. Aborting font load.');
86+
return false;
87+
}
88+
}
8889
_registeredFonts.add(RegisteredFont(list, fontFamily, typeface));
8990
_registerWithFontProvider();
9091
} else {
@@ -197,13 +198,6 @@ class SkiaFontCollection implements FlutterFontCollection {
197198
return FontDownloadResult.fromFont(assetName, UnregisteredFont(fontData, url, fontFamily));
198199
}
199200

200-
String? _readActualFamilyName(Uint8List bytes) {
201-
final SkFontMgr tmpFontMgr = canvasKit.FontMgr.FromData(<Uint8List>[bytes])!;
202-
final String? actualFamily = tmpFontMgr.getFamilyName(0);
203-
tmpFontMgr.delete();
204-
return actualFamily;
205-
}
206-
207201
TypefaceFontProvider? _fontProvider;
208202
SkFontCollection? skFontCollection;
209203

0 commit comments

Comments
 (0)