Skip to content

Commit 988b8f1

Browse files
chunhtaifluttergithubbot
authored andcommitted
Fix FontLoader does not remove the cache in web engine (flutter#14536)
1 parent 1e1f371 commit 988b8f1

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

lib/web_ui/lib/src/engine/text/font_collection.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ class FontManager {
200200
final html.FontFace fontFace = html.FontFace(family, list);
201201
return fontFace.load().then((_) {
202202
html.document.fonts.add(fontFace);
203+
// There might be paragraph measurements for this new font before it is
204+
// loaded. They were measured using fallback font, so we should clear the
205+
// cache.
206+
TextMeasurementService.clearCache();
203207
}, onError: (dynamic exception) {
204208
// Failures here will throw an html.DomException which confusingly
205209
// does not implement Exception or Error. Rethrow an Exception so it can

lib/web_ui/lib/src/engine/text/measurement.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ abstract class TextMeasurementService {
168168
/// Initializes the text measurement service with a specific
169169
/// [rulerCacheCapacity] that gets passed to the [RulerManager].
170170
static void initialize({@required int rulerCacheCapacity}) {
171-
clearCache();
171+
rulerManager?.dispose();
172+
rulerManager = null;
172173
rulerManager = RulerManager(rulerCacheCapacity: rulerCacheCapacity);
173174
}
174175

@@ -211,11 +212,10 @@ abstract class TextMeasurementService {
211212
return domInstance;
212213
}
213214

214-
/// Clears the cache of paragraph rulers.
215-
@visibleForTesting
215+
/// Clears the cache of paragraph rulers that are used for measuring paragraph
216+
/// metrics.
216217
static void clearCache() {
217-
rulerManager?.dispose();
218-
rulerManager = null;
218+
rulerManager?._evictAllRulers();
219219
}
220220

221221
static bool _canUseCanvasMeasurement(EngineParagraph paragraph) {

lib/web_ui/test/text/font_loading_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:typed_data';
77

88
import 'package:test/test.dart';
99
import 'package:ui/ui.dart' as ui;
10+
import 'package:ui/src/engine.dart';
1011

1112
Future<void> main() async {
1213
await ui.webOnlyInitializeTestDomRenderer();
@@ -34,6 +35,29 @@ Future<void> main() async {
3435

3536
expect(_containsFontFamily('Blehm'), true);
3637
});
38+
39+
test('loads font should clear measurement caches', () async {
40+
final ui.ParagraphStyle style = ui.ParagraphStyle();
41+
final ui.ParagraphBuilder builder = ui.ParagraphBuilder(style);
42+
final ui.ParagraphConstraints constraints = ui.ParagraphConstraints(width: 30.0);
43+
builder.addText('test');
44+
final ui.Paragraph paragraph = builder.build();
45+
// Triggers the measuring and verifies the result has been cached.
46+
paragraph.layout(constraints);
47+
expect(TextMeasurementService.rulerManager.rulers.length, 1);
48+
49+
// Now, loads a new font using loadFontFromList. This should clear the
50+
// cache
51+
final html.HttpRequest response = await html.HttpRequest.request(
52+
_testFontUrl,
53+
responseType: 'arraybuffer');
54+
await ui.loadFontFromList(Uint8List.view(response.response),
55+
fontFamily: 'Blehm');
56+
57+
// Verifies the font is loaded, and the cache is cleaned.
58+
expect(_containsFontFamily('Blehm'), true);
59+
expect(TextMeasurementService.rulerManager.rulers.length, 0);
60+
});
3761
});
3862
}
3963

0 commit comments

Comments
 (0)