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

Commit 56044b3

Browse files
committed
delete fontface polyfill
1 parent 487ee66 commit 56044b3

File tree

2 files changed

+2
-116
lines changed

2 files changed

+2
-116
lines changed

lib/web_ui/lib/src/engine/safe_browser_api.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,6 @@ num? parseFloat(String source) {
134134
return result;
135135
}
136136

137-
final bool supportsFontLoadingApi =
138-
js_util.hasProperty(domWindow, 'FontFace');
139-
140137
final bool supportsFontsClearApi =
141138
js_util.hasProperty(domDocument, 'fonts') &&
142139
js_util.hasProperty(domDocument.fonts!, 'clear');

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

Lines changed: 2 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ class HtmlFontCollection implements FontCollection {
4848
'There was a problem trying to load FontManifest.json');
4949
}
5050

51-
if (supportsFontLoadingApi) {
52-
_assetFontManager = FontManager();
53-
} else {
54-
_assetFontManager = _PolyfillFontManager();
55-
}
51+
_assetFontManager = FontManager();
5652

5753
for (final Map<String, dynamic> fontFamily
5854
in fontManifest.cast<Map<String, dynamic>>()) {
@@ -114,13 +110,7 @@ class HtmlFontCollection implements FontCollection {
114110

115111
/// Manages a collection of fonts and ensures they are loaded.
116112
class FontManager {
117-
factory FontManager() {
118-
if (supportsFontLoadingApi) {
119-
return FontManager._();
120-
} else {
121-
return _PolyfillFontManager();
122-
}
123-
}
113+
factory FontManager() = FontManager._;
124114

125115
FontManager._();
126116

@@ -243,104 +233,3 @@ class FontManager {
243233
});
244234
}
245235
}
246-
247-
/// A font manager that works without using the CSS Font Loading API.
248-
///
249-
/// The CSS Font Loading API is not implemented in IE 11 or Edge. To tell if a
250-
/// font is loaded, we continuously measure some text using that font until the
251-
/// width changes.
252-
class _PolyfillFontManager extends FontManager {
253-
_PolyfillFontManager() : super._();
254-
255-
/// A String containing characters whose width varies greatly between fonts.
256-
static const String _testString = 'giItT1WQy@!-/#';
257-
258-
static const Duration _fontLoadTimeout = Duration(seconds: 2);
259-
static const Duration _fontLoadRetryDuration = Duration(milliseconds: 50);
260-
261-
final List<Future<void>> _completerFutures = <Future<void>>[];
262-
263-
@override
264-
Future<void> downloadAllFonts() async {
265-
await Future.wait(_completerFutures);
266-
}
267-
268-
@override
269-
void registerDownloadedFonts() {}
270-
271-
@override
272-
void downloadAsset(
273-
String family,
274-
String asset,
275-
Map<String, String> descriptors,
276-
) {
277-
final DomHTMLParagraphElement paragraph = createDomHTMLParagraphElement();
278-
paragraph.style.position = 'absolute';
279-
paragraph.style.visibility = 'hidden';
280-
paragraph.style.fontSize = '72px';
281-
const String fallbackFontName = 'sans-serif';
282-
paragraph.style.fontFamily = fallbackFontName;
283-
if (descriptors['style'] != null) {
284-
paragraph.style.fontStyle = descriptors['style']!;
285-
}
286-
if (descriptors['weight'] != null) {
287-
paragraph.style.fontWeight = descriptors['weight']!;
288-
}
289-
paragraph.text = _testString;
290-
291-
domDocument.body!.append(paragraph);
292-
final int sansSerifWidth = paragraph.offsetWidth;
293-
294-
paragraph.style.fontFamily = "'$family', $fallbackFontName";
295-
296-
final Completer<void> completer = Completer<void>();
297-
298-
late DateTime fontLoadStart;
299-
300-
void watchWidth() {
301-
if (paragraph.offsetWidth != sansSerifWidth) {
302-
paragraph.remove();
303-
completer.complete();
304-
} else {
305-
if (DateTime.now().difference(fontLoadStart) > _fontLoadTimeout) {
306-
// Let application waiting for fonts continue with fallback.
307-
completer.complete();
308-
// Throw unhandled exception for logging.
309-
throw Exception('Timed out trying to load font: $family');
310-
} else {
311-
Timer(_fontLoadRetryDuration, watchWidth);
312-
}
313-
}
314-
}
315-
316-
final Map<String, String?> fontStyleMap = <String, String?>{};
317-
fontStyleMap['font-family'] = "'$family'";
318-
fontStyleMap['src'] = asset;
319-
if (descriptors['style'] != null) {
320-
fontStyleMap['font-style'] = descriptors['style'];
321-
}
322-
if (descriptors['weight'] != null) {
323-
fontStyleMap['font-weight'] = descriptors['weight'];
324-
}
325-
final String fontFaceDeclaration = fontStyleMap.keys
326-
.map((String name) => '$name: ${fontStyleMap[name]};')
327-
.join(' ');
328-
final DomHTMLStyleElement fontLoadStyle = createDomHTMLStyleElement();
329-
fontLoadStyle.type = 'text/css';
330-
fontLoadStyle.innerHtml = '@font-face { $fontFaceDeclaration }';
331-
domDocument.head!.append(fontLoadStyle);
332-
333-
// HACK: If this is an icon font, then when it loads it won't change the
334-
// width of our test string. So we just have to hope it loads before the
335-
// layout phase.
336-
if (family.toLowerCase().contains('icon')) {
337-
paragraph.remove();
338-
return;
339-
}
340-
341-
fontLoadStart = DateTime.now();
342-
watchWidth();
343-
344-
_completerFutures.add(completer.future);
345-
}
346-
}

0 commit comments

Comments
 (0)