Skip to content

Commit 8821a27

Browse files
authored
[web_ui] Tighten up font fallback code (#164951)
Make one type and many members private Make some fields final
1 parent 04d710d commit 8821a27

File tree

4 files changed

+40
-52
lines changed

4 files changed

+40
-52
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,15 @@ class FontDownloadResult {
255255
}
256256

257257
class SkiaFallbackRegistry implements FallbackFontRegistry {
258-
SkiaFallbackRegistry(this.fontCollection);
258+
SkiaFallbackRegistry(this._fontCollection);
259259

260-
SkiaFontCollection fontCollection;
260+
final SkiaFontCollection _fontCollection;
261261

262262
@override
263263
List<int> getMissingCodePoints(List<int> codeUnits, List<String> fontFamilies) {
264264
final List<SkFont> fonts = <SkFont>[];
265265
for (final String font in fontFamilies) {
266-
final List<SkFont>? typefacesForFamily = fontCollection.familyToFontMap[font];
266+
final List<SkFont>? typefacesForFamily = _fontCollection.familyToFontMap[font];
267267
if (typefacesForFamily != null) {
268268
fonts.addAll(typefacesForFamily);
269269
}
@@ -295,13 +295,13 @@ class SkiaFallbackRegistry implements FallbackFontRegistry {
295295
printWarning('Failed to parse fallback font $familyName as a font.');
296296
return;
297297
}
298-
fontCollection.registeredFallbackFonts.add(
298+
_fontCollection.registeredFallbackFonts.add(
299299
RegisteredFont(buffer.asUint8List(), familyName, typeface),
300300
);
301301
}
302302

303303
@override
304304
void updateFallbackFontFamilies(List<String> families) {
305-
fontCollection.registerDownloadedFonts();
305+
_fontCollection.registerDownloadedFonts();
306306
}
307307
}

engine/src/flutter/lib/web_ui/lib/src/engine/font_fallbacks.dart

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@ class FontFallbackManager {
2424
factory FontFallbackManager(FallbackFontRegistry registry) =>
2525
FontFallbackManager._(registry, getFallbackFontList());
2626

27-
FontFallbackManager._(this.registry, this.fallbackFonts)
28-
: _notoSymbols = fallbackFonts.singleWhere(
27+
FontFallbackManager._(this._registry, this._fallbackFonts)
28+
: _notoSymbols = _fallbackFonts.singleWhere(
2929
(NotoFont font) => font.name == 'Noto Sans Symbols',
3030
) {
31-
downloadQueue = FallbackFontDownloadQueue(this);
31+
_downloadQueue = _FallbackFontDownloadQueue(this);
3232
}
3333

34-
final FallbackFontRegistry registry;
34+
final FallbackFontRegistry _registry;
3535

36-
late final FallbackFontDownloadQueue downloadQueue;
36+
late final _FallbackFontDownloadQueue _downloadQueue;
3737

3838
/// Code points that no known font has a glyph for.
39-
final Set<int> codePointsWithNoKnownFont = <int>{};
39+
final Set<int> _codePointsWithNoKnownFont = <int>{};
4040

4141
/// Code points which are known to be covered by at least one fallback font.
42-
final Set<int> knownCoveredCodePoints = <int>{};
42+
final Set<int> _knownCoveredCodePoints = <int>{};
4343

44-
final List<NotoFont> fallbackFonts;
44+
final List<NotoFont> _fallbackFonts;
4545

4646
// By default, we use the system language to determine the user's preferred
4747
// language. This can be overridden through [debugUserPreferredLanguage] for testing.
@@ -55,6 +55,9 @@ class FontFallbackManager {
5555
_language = value;
5656
}
5757

58+
@visibleForTesting
59+
void Function(String family)? debugOnLoadFontFamily;
60+
5861
final NotoFont _notoSymbols;
5962

6063
Future<void> _idleFuture = Future<void>.value();
@@ -101,8 +104,8 @@ class FontFallbackManager {
101104
for (final int rune in text.runes) {
102105
// Filter out code points that don't need checking.
103106
if (!(rune < 160 || // ASCII and Unicode control points.
104-
knownCoveredCodePoints.contains(rune) || // Points we've already covered
105-
codePointsWithNoKnownFont.contains(rune)) // Points that don't have a fallback font
107+
_knownCoveredCodePoints.contains(rune) || // Points we've already covered
108+
_codePointsWithNoKnownFont.contains(rune)) // Points that don't have a fallback font
106109
) {
107110
runesToCheck.add(rune);
108111
}
@@ -112,7 +115,7 @@ class FontFallbackManager {
112115
}
113116

114117
final List<int> codePoints = runesToCheck.toList();
115-
final List<int> missingCodePoints = registry.getMissingCodePoints(codePoints, fontFamilies);
118+
final List<int> missingCodePoints = _registry.getMissingCodePoints(codePoints, fontFamilies);
116119

117120
if (missingCodePoints.isNotEmpty) {
118121
addMissingCodePoints(codePoints);
@@ -126,7 +129,7 @@ class FontFallbackManager {
126129
_idleFuture = Future<void>.delayed(Duration.zero, () async {
127130
_ensureFallbackFonts();
128131
_scheduledCodePointCheck = false;
129-
await downloadQueue.waitForIdle();
132+
await _downloadQueue.waitForIdle();
130133
});
131134
}
132135
}
@@ -168,7 +171,7 @@ class FontFallbackManager {
168171
/// user's locale.
169172
///
170173
/// If a code point is not covered by any font, it is added to
171-
/// [codePointsWithNoKnownFont] so it can be omitted next time to avoid
174+
/// [_codePointsWithNoKnownFont] so it can be omitted next time to avoid
172175
/// searching for fonts unnecessarily.
173176
void findFontsForMissingCodePoints(List<int> codePoints) {
174177
final List<int> missingCodePoints = <int>[];
@@ -227,18 +230,18 @@ class FontFallbackManager {
227230
candidateFonts.removeWhere((NotoFont font) => font.coverCount == 0);
228231
}
229232

230-
selectedFonts.forEach(downloadQueue.add);
233+
selectedFonts.forEach(_downloadQueue.add);
231234

232235
// Report code points not covered by any fallback font and ensure we don't
233236
// process those code points again.
234237
if (missingCodePoints.isNotEmpty) {
235-
if (!downloadQueue.isPending) {
238+
if (!_downloadQueue.isPending) {
236239
printWarning(
237240
'Could not find a set of Noto fonts to display all missing '
238241
'characters. Please add a font asset for the missing characters.'
239242
' See: https://flutter.dev/docs/cookbook/design/fonts',
240243
);
241-
codePointsWithNoKnownFont.addAll(missingCodePoints);
244+
_codePointsWithNoKnownFont.addAll(missingCodePoints);
242245
}
243246
}
244247
}
@@ -335,7 +338,7 @@ class FontFallbackManager {
335338
if (kFontIndexDigit0 <= code && code < kFontIndexDigit0 + kFontIndexRadix) {
336339
final int delta = prefix * kFontIndexRadix + (code - kFontIndexDigit0);
337340
final int index = previousIndex + delta + 1;
338-
result.add(fallbackFonts[index]);
341+
result.add(_fallbackFonts[index]);
339342
previousIndex = index;
340343
prefix = 0;
341344
} else if (kPrefixDigit0 <= code && code < kPrefixDigit0 + kPrefixRadix) {
@@ -436,20 +439,16 @@ class _UnicodePropertyLookup<P> {
436439
}
437440
}
438441

439-
class FallbackFontDownloadQueue {
440-
FallbackFontDownloadQueue(this.fallbackManager);
442+
class _FallbackFontDownloadQueue {
443+
_FallbackFontDownloadQueue(this.fallbackManager);
441444

442445
final FontFallbackManager fallbackManager;
443446

444-
String get fallbackFontUrlPrefix => configuration.fontFallbackBaseUrl;
445-
446447
final Set<NotoFont> downloadedFonts = <NotoFont>{};
447448
final Map<String, NotoFont> pendingFonts = <String, NotoFont>{};
448449

449450
bool get isPending => pendingFonts.isNotEmpty;
450451

451-
void Function(String family)? debugOnLoadFontFamily;
452-
453452
Completer<void>? _idleCompleter;
454453

455454
Future<void> waitForIdle() {
@@ -478,17 +477,14 @@ class FallbackFontDownloadQueue {
478477
final List<String> downloadedFontFamilies = <String>[];
479478
for (final NotoFont font in pendingFonts.values) {
480479
downloads[font.url] = Future<void>(() async {
480+
final String url = '${configuration.fontFallbackBaseUrl}${font.url}';
481481
try {
482-
final String url = '$fallbackFontUrlPrefix${font.url}';
483-
debugOnLoadFontFamily?.call(font.name);
484-
await fallbackManager.registry.loadFallbackFont(font.name, url);
482+
fallbackManager.debugOnLoadFontFamily?.call(font.name);
483+
await fallbackManager._registry.loadFallbackFont(font.name, url);
485484
downloadedFontFamilies.add(font.url);
486485
} catch (e) {
487486
pendingFonts.remove(font.url);
488-
printWarning(
489-
'Failed to load font ${font.name} at '
490-
'$fallbackFontUrlPrefix${font.url}',
491-
);
487+
printWarning('Failed to load font ${font.name} at $url');
492488
printWarning(e.toString());
493489
return;
494490
}
@@ -508,7 +504,7 @@ class FallbackFontDownloadQueue {
508504
}
509505

510506
if (pendingFonts.isEmpty) {
511-
fallbackManager.registry.updateFallbackFontFamilies(fallbackManager.globalFontFallbacks);
507+
fallbackManager._registry.updateFallbackFontFamilies(fallbackManager.globalFontFallbacks);
512508
sendFontChangeMessage();
513509
final Completer<void> idleCompleter = _idleCompleter!;
514510
_idleCompleter = null;

engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/font_collection.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import 'dart:async';
66
import 'dart:ffi';
77
import 'dart:js_interop';
8-
98
import 'dart:typed_data';
109

1110
import 'package:ui/src/engine.dart';
@@ -193,16 +192,16 @@ class SkwasmFontCollection implements FlutterFontCollection {
193192
}
194193

195194
class SkwasmFallbackRegistry implements FallbackFontRegistry {
196-
SkwasmFallbackRegistry(this.fontCollection);
195+
SkwasmFallbackRegistry(this._fontCollection);
197196

198-
final SkwasmFontCollection fontCollection;
197+
final SkwasmFontCollection _fontCollection;
199198

200199
@override
201200
List<int> getMissingCodePoints(List<int> codePoints, List<String> fontFamilies) =>
202201
withStackScope((StackScope scope) {
203202
final List<SkwasmTypeface> typefaces =
204203
fontFamilies
205-
.map((String family) => fontCollection.registeredTypefaces[family])
204+
.map((String family) => _fontCollection.registeredTypefaces[family])
206205
.fold(
207206
const Iterable<SkwasmTypeface>.empty(),
208207
(Iterable<SkwasmTypeface> accumulated, List<SkwasmTypeface>? typefaces) =>
@@ -229,9 +228,9 @@ class SkwasmFallbackRegistry implements FallbackFontRegistry {
229228

230229
@override
231230
Future<void> loadFallbackFont(String familyName, String url) =>
232-
fontCollection.loadFontFromUrl(familyName, url);
231+
_fontCollection.loadFontFromUrl(familyName, url);
233232

234233
@override
235234
void updateFallbackFontFamilies(List<String> families) =>
236-
fontCollection.setDefaultFontFamilies(families);
235+
_fontCollection.setDefaultFontFamilies(families);
237236
}

engine/src/flutter/lib/web_ui/test/ui/fallback_fonts_golden_test.dart

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'dart:math' as math;
77

88
import 'package:test/bootstrap/browser.dart';
99
import 'package:test/test.dart';
10-
1110
import 'package:ui/src/engine.dart';
1211
import 'package:ui/ui.dart' as ui;
1312
import 'package:web_engine_tester/golden_tester.dart';
@@ -44,7 +43,7 @@ void testMain() {
4443
<String, Object?>{'fontFallbackBaseUrl': 'assets/fallback_fonts/'}.jsify()
4544
as JsFlutterConfiguration?,
4645
);
47-
renderer.fontCollection.fontFallbackManager!.downloadQueue.debugOnLoadFontFamily =
46+
renderer.fontCollection.fontFallbackManager!.debugOnLoadFontFamily =
4847
(String family) => downloadedFontFamilies.add(family);
4948
savedCallback = ui.PlatformDispatcher.instance.onPlatformMessage;
5049
});
@@ -59,19 +58,13 @@ void testMain() {
5958
});
6059

6160
test('can override font fallback base URL using JS', () {
62-
expect(
63-
renderer.fontCollection.fontFallbackManager!.downloadQueue.fallbackFontUrlPrefix,
64-
'assets/fallback_fonts/',
65-
);
61+
expect(configuration.fontFallbackBaseUrl, 'assets/fallback_fonts/');
6662
debugOverrideJsConfiguration(
6763
<String, Object?>{'fontFallbackBaseUrl': 'http://my-special-fonts.com/'}.jsify()
6864
as JsFlutterConfiguration?,
6965
);
7066

71-
expect(
72-
renderer.fontCollection.fontFallbackManager!.downloadQueue.fallbackFontUrlPrefix,
73-
'http://my-special-fonts.com/',
74-
);
67+
expect(configuration.fontFallbackBaseUrl, 'http://my-special-fonts.com/');
7568
});
7669

7770
test('will download Noto Sans Arabic if Arabic text is added', () async {

0 commit comments

Comments
 (0)