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

Commit dd12a40

Browse files
authored
delete fontface polyfill (#37652)
* delete fontface polyfill * removed fonts clear and factory constructor * addressed comments
1 parent bc86c1f commit dd12a40

File tree

2 files changed

+2
-126
lines changed

2 files changed

+2
-126
lines changed

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

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

137-
final bool supportsFontLoadingApi =
138-
js_util.hasProperty(domWindow, 'FontFace');
139-
140-
final bool supportsFontsClearApi =
141-
js_util.hasProperty(domDocument, 'fonts') &&
142-
js_util.hasProperty(domDocument.fonts!, 'clear');
143-
144137
/// Used to decide if the browser tab still has the focus.
145138
///
146139
/// This information is useful for deciding on the blur behavior.

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

Lines changed: 2 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import 'package:ui/src/engine/fonts.dart';
1010

1111
import '../assets.dart';
1212
import '../dom.dart';
13-
import '../safe_browser_api.dart';
1413
import '../util.dart';
1514
import 'layout_service.dart';
1615

@@ -48,11 +47,7 @@ class HtmlFontCollection implements FontCollection {
4847
'There was a problem trying to load FontManifest.json');
4948
}
5049

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

5752
for (final Map<String, dynamic> fontFamily
5853
in fontManifest.cast<Map<String, dynamic>>()) {
@@ -106,23 +101,12 @@ class HtmlFontCollection implements FontCollection {
106101
void clear() {
107102
_assetFontManager = null;
108103
_testFontManager = null;
109-
if (supportsFontsClearApi) {
110-
domDocument.fonts!.clear();
111-
}
104+
domDocument.fonts!.clear();
112105
}
113106
}
114107

115108
/// Manages a collection of fonts and ensures they are loaded.
116109
class FontManager {
117-
factory FontManager() {
118-
if (supportsFontLoadingApi) {
119-
return FontManager._();
120-
} else {
121-
return _PolyfillFontManager();
122-
}
123-
}
124-
125-
FontManager._();
126110

127111
/// Fonts that started the downloading process. Once the fonts have downloaded
128112
/// without error, they are moved to [_downloadedFonts]. Those fonts
@@ -243,104 +227,3 @@ class FontManager {
243227
});
244228
}
245229
}
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)